Properties, Class & Static Methods in Python
Chapter 23: Properties, Class & Static Methods
4 short lessons, with 3D animations, quizzes and coding tasks.
The lessons
@property: values that work themselves out
@property turns a method into something you read like an attribute, rect.area with no brackets, worked out fresh each time.
Setters: a guard at the gate
A setter (@name.setter) runs every time someone writes obj.name = value, so it can check or tidy the value before storing it.
@classmethod: asking the class itself
A method marked @classmethod is called on the class and gets the class itself as cls, which makes it perfect for alternative ways to build objects.
@staticmethod, and choosing the right kind
A @staticmethod is a plain function kept inside a class because it belongs there; it gets neither self nor cls.