Lesson · Object-Oriented Python
Instance, Class & Static Methods
55 min readInstance, Class & Static Methods
Instance methods receive an instance, class methods receive the class, and static methods receive neither automatically. The method kind should match the object whose state or construction policy the behavior needs.
Learning goals
- Explain what each method kind receives automatically:
selffor instance methods,clsfor class methods, neither for static methods. - Trace
Temperature.freezing()from the classmethod call throughcls(0)to the instance thatdisplay()prints. - Decide which kind fits a behavior by asking whose state it needs: the instance, the class, or nothing at all.
- Predict why
cls(0)keeps subclasses working while a hard-codedTemperature(0)would not. - Classify
c_to_fcorrectly: close to the class, but receiving no object at all.
Mental model
The deciding question is what the behavior needs to do its job: touch per-instance data, respect the class and its subclasses, or neither. The kind you choose is the smallest one that still receives the object it actually uses.
Mental model
read it top to bottom
does the behavior need this instance's data?
↓ yes → instance method (bound to self)
↓ no: does it need the class itself (construction, cls)?
↓ yes → @classmethod (bound to cls)
↓ no → @staticmethod (no automatic argument)Core idea
The three method kinds differ only in what the call binds automatically: self for a normal method, cls for a class method, and nothing for a static method. Choose the smallest binding the behavior needs, because cls is what lets an alternate constructor return the right subclass, and a static method is the honest choice for an operation that could live at module level. The method kind is a statement about which object owns the behavior.
Instance methods
An instance method is a function stored on the class that receives the instance as its first argument, conventionally self. Accessing it through an object returns a bound method, so account.deposit(50) calls deposit(account, 50) without you passing the instance. Use it when the behavior reads or writes per-instance state.
@classmethod
A class method receives the class as its first argument. Alternate constructors are a common use because subclasses automatically receive themselves as cls.
@staticmethod
A static method is stored on a class but receives no automatic instance or class argument. Use it only when the operation belongs conceptually near the class but needs neither object state nor construction policy.
Alternate constructors
An alternate constructor is commonly a class method that converts another representation into an instance. It keeps construction policy on the class while preserving subclass behavior.
Utility methods
A utility method is a function that lives on the class for proximity but never touches the instance's or the class's state; c_to_f only transforms its argument. Declare it with @staticmethod so the call passes no automatic argument and the signature states the truth. If it does not belong to the class conceptually at all, a module-level function is equally valid.
Walkthrough
def display(self)is an instance method:Temperature.freezing().display()binds the new instance toself, and the f-string readsself.value(which is0) plusself.scale, found on the class because no instance sets it.@classmethod def freezing(cls)receives the class itself;Temperature.freezing()passesTemperatureascls, andreturn cls(0)builds an instance of whichever class the call came through.@staticmethod def c_to_f(celsius)is called asTemperature.c_to_f(100)and receives only100; it never touchesself.valueorcls.scale, which is exactly why it can be static.- Predict before running: the two printed lines, what
clsis insidefreezing, and what changes if aclass Fahrenheit(Temperature)subclass calls.freezing().
Experiments to try
- Add
class Fahrenheit(Temperature): scale = "F"and callFahrenheit.freezing().display(); explain whatcls(0)built and why no new constructor code was needed. - Call
Temperature.display(Temperature.freezing())and confirm it behaves like the bound call, then say what the dot normally supplies. - Add a
boiling(cls)classmethod next tofreezingand decide whether it needsclsat all if it returns a value that never varies. - Move
c_to_fout of the class to a module-level function and back; decide which location you would keep and what the class placement communicates.
Common mistakes
- Decorating a method that uses
selfwith@staticmethod: no instance is passed, so the first argument is missing or receives the wrong value and the call raisesTypeError. - Writing
@classmethodfor a method that never usescls: the decorator claims a dependency on the class that does not exist, so@staticmethod(or a module-level function) states the truth. - Hard-coding the class name in an alternate constructor, as in
return Temperature(0)insidefreezing: a subclass then receives a base-class instance, which defeats the point ofcls. - Calling an instance method without an instance:
Temperature.display()raisesTypeErrorbecause nothing was bound toself.
Practice: Easy
- Run the worked example, then call
Temperature.freezing()twice and print bothdisplay()results to confirm each call builds a fresh instance rather than reusing one. - Change
freezingto returnTemperature(0)instead ofcls(0), addclass Fahrenheit(Temperature): scale = "F", and compareFahrenheit.freezing().display()before and after the change. - Call
Temperature.c_to_f(0),Temperature.c_to_f(100), andTemperature.c_to_f(-40); record the outputs and confirm the method needs no instance by calling it before constructing anyTemperature.
Practice: Medium
- Add a
from_fahrenheit(cls, value)classmethod that converts backwards and returnscls(...). Check the returned type withtype(...)and the scale it prints when called on a subclass. - Give
Temperatureanaverage(values)static method that takes a list of numbers and returns their mean. Explain in one sentence why it should not be a@classmethodeven though it sits on the class. - Convert
displayinto a property and list every call site that must lose its parentheses. Explain why the property still receivesself.
Practice: Hard
- Design a
Vectorclass with an instance methodadd(self, other), a classmethodzero(cls)that builds a zero vector, and a static methoddot(a, b). Write tests that pin what each kind receives before implementing. - Build a failure lab: call an instance method with no instance, decorate a
self-using method as@staticmethod, and hard-code the class name in an alternate constructor. Record each error or wrong result and the rule it breaks. - Connect this lesson to the inheritance lesson: give
Temperaturea subclass that overrides__init__with an extra parameter, then decide which alternate constructors still work unchanged and which need overriding, and explain whatclshas to do with it.
Lesson checklist
- I can name what each of the three method kinds receives when it is called.
- I can trace
cls(0)insidefreezingand say which class it used. - I can classify a new behavior as instance method, classmethod, or staticmethod by what it touches.
- I can explain why a
self-using method breaks under@staticmethod.
Remember
Instance methods bind `self`, class methods bind `cls`, and static methods bind nothing; the kind should be the smallest binding the behavior actually uses. `cls` is what makes an alternate constructor return the subclass that called it, and a method that touches neither the instance nor the class is more honest as a static method or a module-level function.
Interview angles
What is the main purpose of **Instance methods**?+
An instance method is a function stored on the class that receives the instance as its first argument, conventionally self. Accessing it through an object returns a bound method, so account.deposit(50) calls deposit(account, 50) without you passing the instance. Use it when the behavior reads or writes per-instance state.
What should you pay attention to when using **@classmethod**?+
A class method receives the class as its first argument. Alternate constructors are a common use because subclasses automatically receive themselves as cls.
How does **@staticmethod** fit into this lesson?+
A static method is stored on a class but receives no automatic instance or class argument. Use it only when the operation belongs conceptually near the class but needs neither object state nor construction policy.
Why does `@classmethod` matter for an alternate constructor such as `freezing`, and what breaks without it?+
The classmethod receives the class the call came through as cls, so return cls(0) builds an instance of that exact class. Hard-coding Temperature(0) would always build a base-class instance, so a Fahrenheit subclass would silently lose its own scale.