A static method in Python is bound to the class itself rather than any instance of that class. So, you can call it without first creating an object and without access to instance data (self
).
To create a static method we need to use a decorator, specifically @staticmethod
. It will tell Python to call the method on the class rather than an instance.
Static methods are excellent for utility or helper functions that are logically connected to the class but don't need to access any of its properties.
Static methods are frequently used in real-world code for tasks like input validation, data formatting, and calculations—especially when that logic naturally belongs with a class but doesn't need its state.
Here's an example from a User
class that checks email format:
class User:
@staticmethod
def is_valid_email(email):
return "@" in email and "." in email
This method doesn't depend on any part of the User
instance, but conceptually belongs in the class. It can be used anywhere as User.is_valid_email(email)
, keeping your code cleaner and more organized.
If the logic requires access to or modification of instance attributes or class-level data, avoid using a static method as it won't help here. For instance, if you are working with user settings or need to monitor object creation, you will require a class method or an instance method instead.
class Counter:
count = 0
@classmethod
def increment(cls):
cls.count += 1
In this example, using a static method would prevent access to cls.count
, making it useless for this kind of task.
Though they look similar, class and static methods in Python have different uses; so, let's now quickly review their differences.
Defined inside a class, a class method is connected to that class rather than an instance. Conventionally called cls
, the class itself is the first parameter; so, it can access and change class-level data. Factory patterns, alternate constructors, or any activity applicable to the class as a whole and not individual instances are often implemented via class methods.
Conversely, a static method is defined within a class but does not start with either self
or cls
parameters. It is just a regular function that “lives” inside a class but doesn’t interact with the class or its instances. For utility tasks that are conceptually related to the class but don’t depend on its state, static methods are perfect.
Here's a quick breakdown of the Python class/static methods differences:
Feature |
Class Method |
Static Method |
Binding |
Bound to the class |
Not bound to class or instance |
First parameter |
cls (class itself) |
None (no self or cls) |
Access to class/instance data |
Yes |
No |
Common use cases |
Factory methods, class-level behavior |
Utility/helper functions |
Decorator |
|
|
You might ask: why not just define a function outside the class instead of using a static method? The answer is structure. A static method keeps related logic grouped within the class, even if it doesn't interact with the class or its instances.
# Regular function
def is_even(n):
return n % 2 == 0
# Static method inside a class
class NumberUtils:
@staticmethod
def is_even(n):
return n % 2 == 0
Both functions do the same thing, but placing is_even inside NumberUtils
helps keep utility logic organized and easier to find later.
Let’s proceed to the hands-on Python static method examples.
Imagine that we have a MathUtils
class that contains a static method for calculating the factorial:
class MathUtils:
@staticmethod
def factorial(n):
if n == 0:
return 1
else:
return n * MathUtils.factorial(n-1)
Next, let's enter:
print(MathUtils.factorial(5))
120
We get the factorial of 5, which is 120. Here, the factorial
static method does not use any attributes of the class instance, only the input argument n
. And we called it using the MathUtils.factorial(n)
syntax without creating an instance of the MathUtils
class.
In Python, static methods apply not only in classes but also in modules and packages. The @staticmethod
decorator marks a function you define inside a class if it does not interact with instance-specific data. The function exists on its own; it is related to the class logically but is independent of its internal state.
Managed solution for Backend development
Let's say we're working with a StringUtils
module with a static method for checking if a string is a palindrome. The code will be:
def is_palindrome(string):
return string == string[::-1]
This function doesn't rely on any instance-specific data — it simply performs a check on the input. That makes it a good candidate for a static method. To organize it within a class and signal that it doesn't depend on the class state, we can use the @staticmethod
decorator like this:
class StringUtils:
@staticmethod
def is_palindrome(string):
return string == string[::-1]
Let's enter for verification:
print(StringUtils.is_palindrome("deed"))
True
print(StringUtils.is_palindrome("deer"))
False
That's correct, the first word is a palindrome, so the interpreter outputs True
, but the second word is not, and we get False
.
So, we can call the is_palindrome
method through the StringUtils
class using the StringUtils.is_palindrome(string)
syntax instead of importing the is_palindrome
function and calling it directly.
-
Python static method and class instance also differ in that the static cannot affect the state of an instance. Since they do not have access to the instance, they cannot alter attribute values, which makes sense. Instance methods are how one may modify the instance state of a class.
Let's look at another example. Suppose we have a Person
class that has an age
attribute and a static is_adult
method that checks the value against the age of majority:
class Person:
def __init__(self, age):
self.age = age
@staticmethod
def is_adult(age):
return age >= 21
Next, let's create an age
variable with a value of 24
, call the is_adult
static method from the Person
class with this value and store its result in the is_adult
variable, like this:
age = 24
is_adult = Person.is_adult(age)
Now to test this, let's enter:
print(is_adult)
True
Since the age matches the condition specified in the static method, we get True
. In the example, the is_adult
static method serves as an auxiliary tool—a helper function—accepting the age
argument but without access to the age
attribute of the Person
class instance.
Static methods improve code readability and make it possible to reuse it. They are also more convenient when compared to standard Python functions. Static methods are convenient as, unlike functions, they do not call for a separate import. Therefore, applying Python class static methods can help you streamline and work with your code greatly. And, as you've probably seen from the examples above, they are quite easy to master.
On our app platform you can find Python applications, such as Celery, Django, FastAPI and Flask.