Python 3D Academyবাংলা

Properties, Class & Static Methods in Python

Chapter 23: Properties, Class & Static Methods

4 short lessons, with 3D animations, quizzes and coding tasks.

The lessons

  1. @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.

  2. 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.

  3. @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.

  4. @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.