Sentry Answers>Python>

Difference between `@staticmethod` and `@classmethod` function decorators in Python

Difference between `@staticmethod` and `@classmethod` function decorators in Python

David Y.

The ProblemJump To Solution

What is the difference between a function decorated with @staticmethod and one decorated with @classmethod in Python?

The Solution

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:

Click to Copy
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.

Click to Copy
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.

Click to Copy
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.

  • Sentry BlogPython Performance Testing: A Comprehensive Guide
  • Sentry BlogLogging in Python: A Developer’s Guide
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Loved by over 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.

© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.