Difference between `@staticmethod` and `@classmethod` function decorators in Python
David Y.
—What is the difference between a function decorated with @staticmethod
and one decorated with @classmethod
in Python?
Python programmers who have used classes will know that standard methods inside a class receive the called instance as their first argument. This is conventionally called self
and must be specified as a parameter, per the Python principle that “explicit is better than implicit”. The following code snippet shows an example of this:
class Greeter: def __init__(self, name): self.name = name def say_hello(self): print(f"Hello, my name is {self.name}.") alice = Greeter("Alice") alice.say_hello() # will print "Hello, my name is Alice."
Functions decorated by @staticmethod
or @classmethod
provide alternatives to this behavior. We can decorate a function with @staticmethod
to prevent Python from passing an instance of the object to it, as below.
class Greeter: def __init__(self, name): self.name = name @staticmethod def say_hello_static(name): # <-- no self parameter print(f"Hello {name}, how are you?") alice = Greeter("Alice") alice.say_hello_static("Bob") # will print "Hello Bob, how are you?"
This can be useful when we have functionality that logically belongs in a given class, but does not do anything with the instance it’s called on.
By contrast, the @classmethod
decorator will make Python pass the class of the instance it’s called on as the first argument. By convention, the first parameter of a class method is called cls
. Class methods can also be called on classes directly.
class Greeter: @classmethod def say_hello_class(cls): # <-- cls instead of self print(f"Hello, I am a {cls.__name__}.") Greeter.say_hello_class() # will print "Hello, I am a Greeter." alice = Greeter() alice.say_hello_class() # will print "Hello, I am a Greeter."
Class methods are useful when we want to define behaviors that affect a class as a whole, rather than single instances. A common use case is creating factory methods.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.