A robot with a battery: composition, class variables, staticmethod, classmethod.
In one line
Composition means keeping another object as an attribute and handing it work. A class variable lives once on the class rather than on each object, and @staticmethod and @classmethod are methods you call without an object.
Why you need it
Inherit with class Robot(Battery) and the robot exposes level and use() directly. The moment you want two batteries or a different kind, the inheritance line has to be torn out.
Keep the battery as an attribute and the robot only needs self.battery.use(). Swap in another battery class and the robot code stays the same.
Everyday picture
Think of a cordless drill. The drill has a battery pack, and when it runs flat you clip in another. If the drill had inherited from the battery, a dead battery would mean a dead drill.
Press Run to try your edited code right away. The step-through and the output above follow the original code.
When to use it
"Has a" fits: a robot and its battery, an order and its address, a game character and its bag
A value every object should share (a counter, a setting): use a class variable
Helpers that need no object: @staticmethod; another way to build objects from different input: @classmethod
In Python, a helper used across several modules is more often a plain function outside the class. Keep @staticmethod for helpers that clearly belong to that one class.
Common mistakes
Keeping a mutable list as a class variable
tools = [] in the class body is one list shared by every robot. Values each robot should own go in __init__ with self..
self.made += 1 reads the class value, adds 1 and stores the result as a new attribute on the object. Robot.made stays 0. Change it through the class name.
Wrong
classRobot: made = 0def__init__(self):self.made += 1Robot()Robot()print(Robot.made)
When you run it0
Fixed
classRobot: made = 0def__init__(self):Robot.made += 1Robot()Robot()print(Robot.made)
When you run it2
Forgetting @staticmethod
Without the decorator it is a normal method, so the object is slipped in as the first argument. Helpers that need no object get @staticmethod.
Answer: 2. 2from_text also goes through __init__ via cls(...). Each call adds 1, so it is 2.
Question 2
Which decorator passes the class itself as the first argument?
Show the answer
Answer: 2. @classmethod@classmethod receives cls. Called on a subclass, cls is that subclass, so constructors like from_text keep working for children.
Questions people ask
What is the difference between @staticmethod and @classmethod?
@staticmethod receives nothing automatically; @classmethod receives the class as cls. Use @classmethod for alternative ways to build objects.
Why is a list as a class variable a problem?
Every object shares that single list, so a value added through one object shows up in all of them. Create per-object values in __init__.
Is composition always better than inheritance?
No. When "is a" clearly fits, inheritance is tidy. For parts you may swap or have several of, composition fits.