Python Class Methods and Static Methods
Learn how Python class methods receive the class, how static methods receive no implicit object, and how to choose between method styles.
Intermediate
Python has three useful method styles: ordinary instance methods receive the instance, class methods receive the class, and static methods receive no implicit object. Choosing the right style makes a class interface easier to understand.
An ordinary method receives the instance
A normal method is called through an instance, so Python supplies that instance as the first argument, conventionally named self.
|
|
describe() is an instance method because it works with one particular Lamp object.
A class method receives the class
Apply @classmethod when the method needs the class itself. Python supplies the class as the first argument, conventionally named cls, whether the method is called through the class or an instance.
|
|
Both calls supply Ticket as cls; an instance does not become the first argument.
Use a class method for alternate constructors
A class method can construct an instance by calling cls(...). This keeps the construction path compatible with subclasses: a subclass calling the inherited class method receives the subclass as cls.
|
|
from_email() provides an alternate constructor while still using the class that called it.
The class passed to a class method can be a subclass
Because cls is the class used for the call, an inherited class method can create the right subclass without hard-coding the base class name.
|
|
Alert.create() passes Alert as cls, so cls(text) creates an Alert instance.
A static method receives no implicit argument
Apply @staticmethod when a function belongs naturally to the class namespace but does not need either an instance or the class. Python does not insert self or cls.
|
|
The static method gets only the argument you explicitly pass.
Class and static methods can be called on an instance
Both styles can be accessed through a class or an instance. The difference is what Python supplies implicitly: cls for a class method and nothing for a static method.
|
|
Accessing either method through formatter still does not turn the static method into an instance method.
Use class methods when the class matters
A class method is a good fit for behavior that reads or constructs through class-level state. Using cls instead of a hard-coded class name also lets inherited calls respect subclasses.
|
|
The same class method respects the extension defined by the class used for the call.
Use static methods when neither object nor class state matters
A static method is useful for a helper whose concept belongs with the class but whose calculation depends only on its explicit arguments. If a helper is equally unrelated to the class, a module-level function may be clearer.
|
|
valid_quantity() needs neither self nor cls, so staticmethod expresses that dependency clearly.
Compare the three method styles
Choose the method style by asking what the operation needs: instance state, class state, or only its explicit arguments. The syntax changes how Python binds the first argument.
|
|
Instance methods receive self, class methods receive cls, and static methods receive no implicit argument.
Key takeaways
- An instance method receives the instance as self.
- A class method receives the class as cls and can be called through a class or instance.
- A class method is useful for class-aware behavior and alternate constructors that should work with subclasses.
- A static method receives no implicit self or cls; it uses only the arguments you pass explicitly.
- Choose the method style based on whether the behavior needs instance state, class state, or neither.
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.