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

David Y.

The Problem

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:

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.

Get Started With Sentry

Get actionable, code-level insights to resolve Python performance bottlenecks and errors.

  1. Create a free Sentry account

  2. Create a Python project and note your DSN

  3. Grab the Sentry Python SDK

pip install --upgrade sentry-sdk
  1. Configure your DSN
import sentry_sdk sentry_sdk.init( "https://<key>@sentry.io/<project>", # Set traces_sample_rate to 1.0 to capture 100% # of transactions for performance monitoring. # We recommend adjusting this value in production. traces_sample_rate=1.0, )

Check our documentation for the latest instructions.

Loved by over 4 million developers and more than 90,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.

Share on Twitter
Bookmark this page
Ask a questionJoin the discussion

Related Answers

A better experience for your users. An easier life for your developers.

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