Yep, using the staticmethod decorator:

class MyClass(object):
    @staticmethod
    def the_static_method(x):
        print(x)

MyClass.the_static_method(2)  # outputs 2

Note that some code might use the old method of defining a static method, using staticmethod as a function rather than a decorator. This should only be used if you have to support ancient versions of Python (2.2 and 2.3):

class MyClass(object):
    def the_static_method(x):
        print(x)
    the_static_method = staticmethod(the_static_method)

MyClass.the_static_method(2)  # outputs 2

This is entirely identical to the first example (using @staticmethod), just not using the nice decorator syntax.

Finally, use staticmethod sparingly! There are very few situations where static-methods are necessary in Python, and I've seen them used many times where a separate "top-level" function would have been clearer.


The following is verbatim from the documentation::

A static method does not receive an implicit first argument. To declare a static method, use this idiom:

class C:
    @staticmethod
    def f(arg1, arg2, ...): ...

The @staticmethod form is a function decorator – see the description of function definitions in Function definitions for details.

It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class.

Static methods in Python are similar to those found in Java or C++. For a more advanced concept, see classmethod().

For more information on static methods, consult the documentation on the standard type hierarchy in The standard type hierarchy.

New in version 2.2.

Changed in version 2.4: Function decorator syntax added.

Answer from dbr on Stack Overflow
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-static-method
Python staticmethod: When and How to Use Static Methods | DigitalOcean
September 16, 2025 - The default method type in Python classes. Receives the instance (self) as the first argument. Can access and modify both instance and class variables. Used for behavior that depends on the state of a specific object. ... Use static methods for helpers that are conceptually part of a class’s ...
🌐
Tutorialspoint
tutorialspoint.com › python › python_static_methods.htm
Python - Static Methods
class Employee: empCount = 0 def ... following result − ... The second way to create a static method is by using the Python @staticmethod decorator....
Discussions

What is "static method" in python - Stack Overflow
I'm quite new to python, and could not understand what is static method in python(for example __new__()) and what does it do. Can anyone possibly explain it? Thanks a million More on stackoverflow.com
🌐 stackoverflow.com
what is @staticmethod for?
The difference is only apparent when accessing the method through an instance of A, i.e. A().f(1, 2). f, being a static method, would behave exactly the same as when accessing it through the class. g, being a regular instance method, would automatically be passed the instance as the argument for x. So A().g(1, 2) would result in a TypeError, as it'd receive a total of three arguments. There's a final type of method, class methods using the @classmethod decorator, that are automatically passed a reference to the class they're accessed through, whether that's through the class directly or via one of its instances: class A: @classmethod def h(cls, x, y): return x + y A.h(1, 2) # cls = A A().h(1, 2) # same thing That's useful if the method call happens on a subclass of A, since the argument passed to h would be that subclass. To be honest, in most situations something should either be a class method, an instance method, or not a method, at all. In my experience, there's very few reasons to have static methods. More on reddit.com
🌐 r/learnpython
25
22
April 28, 2024
Not understanding this recursion lesson from W3schools
return 0 is the base case - which is only called when k is 0 (i.e. when you've finished recursing and are at the "deepest" level of recursion). we then start to unwind, completing all the method calls that were waiting on the base case until we reach the initial level and are able to print out the actual sum follow the steps that the program is performing. note how we DON'T actually return from any method until k = 0 is reached: initially, k = 10, so we try to perform 10 + sum(9) - we do NOT YET return here, because we have invoked sum(9) now k = 9, so we try to perform 9 + sum(8) - again, we do NOT YET return here, because we have invoked sum(8) now k = 8, so we try to perform 8 + sum(7) - same as above now k = 7, so we try to perform 7 + sum(6) - same as above now k = 6, so we try to perform 6 + sum(5) - same as above now k = 5, so we try to perform 5 + sum(4) - same as above now k = 4, so we try to perform 4 + sum(3) - same as above now k = 3, so we try to perform 3 + sum(2) - same as above now k = 2, so we try to perform 2 + sum(1) - same as above now k = 1, so we try to perform 1 + sum(0) - same as above now k = 0 - THIS is the point that we're unwinding and returning the results, because we do not call sum() so we've stopped recursing sum(0) = 0 sum(1) = 1 + 0 sum(2) = 2 + 1 + 0 sum(3) = 3 + 2 + 1 + 0 sum(4) = 4 + 3 + 2 + 1 + 0 sum(5) = 5 + 4 + 3 + 2 + 1 + 0 sum(6) = 6 + 5 + 4 + 3 + 2 + 1 + 0 sum(7) = 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0 sum(8) = 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0 sum(9) = 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0 sum(10) = 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0 Now do the math - sum(10) = 55 More on reddit.com
🌐 r/javahelp
6
6
February 11, 2023
CLASS & STATIC METHODS
Classes for Beginners A lot of beginners struggle to get their heads around classes, but they are pretty much fundamental to object orientated programming. I usually describe them as the programming equal of moulds used in factories as a template for making lots of things that are identical. Imagine pouring molten iron into a mould to make a simple iron pot. You might produce a set of instructions to be sold with the pots that tell the owner how to cook using the pot, how to care for it, etc. The same instructions apply to every pot BUT what owners actually do is entirely up to them. Some might make soup, another person a stew, etc. In Python, a class defines the basics of a possible object and some methods that come with it. (Methods are like functions, but apply to things made using the class.) When we want create a Python object using a class, we call it 'creating an instance of a class'. If you have a class called Room, you would create instances like this: lounge = Room() kitchen = Room() hall = Room() As you typically want to store the main dimensions (height, length, width) of a room, whatever it is used for, it makes sense to define that when the instance is created. You would therefore have a method called __init__ that accepts height, length, width and when you create an instance of Room you would provide that information: lounge = Room(1300, 4000, 2000) The __init__ method is called automatically when you create an instance. It is short for initialise (intialize). You can reference the information using lounge.height and so on. These are attributes of the lounge instance. I provided the measurements in mm but you could include a method (function inside a class) that converts between mm and ft. Thus, I could say something like lounge.height_in_ft(). Methods in classes are usually defined with a first parameter of self: def __init__(self, height, length, width): # code for __init__ def height_in_ft(self): # code to return height The self is a shorthand way of referring to an instance. When you use lounge.height_in_ft() the method knows that any reference to self means the lounge instance, so self.height means lounge.height but you don't have to write the code for each individual instance. Thus kitchen.height_in_ft() and bathroom.height_in_ft() use the same method, but you don't have to pass the height of the instance as the method can reference it using self.height EXAMPLE Room class The code shown as the end of this post will generate the following output: Lounge 1300 4000 4000 Snug 1300 2500 2500 Lounge length in feet: 4.26509187 Snug wall area: 11700000 in sq.mm., 125.94 in sq.ft. Note that a method definition that is preceded by the command, @staticmethod (a decorator) is really just a function that does not include the self reference to the calling instance. It is included in a class definition for convenience and can be called by reference to the class or the instance: Room.mm_to_ft(mm) lounge.mm_to_ft(mm) Here's the code for the full programme: class Room(): def __init__(self, name, height, length, width): self.name = name self.height = height self.length = length self.width = width @staticmethod def mm_to_ft(mm): return mm * 0.0032808399 @staticmethod def sqmm_to_sqft(sqmm): return sqmm * 1.07639e-5 def height_in_ft(self): return Room.mm_to_ft(self.height) def width_in_ft(self): return Room.mm_to_ft(self.width) def length_in_ft(self): return Room.mm_to_ft(self.length) def wall_area(self): return self.length * 2 * self.height + self.width * 2 * self.height lounge = Room('Lounge', 1300, 4000, 2000) snug = Room('Snug', 1300, 2500, 2000) print(lounge.name, lounge.height, lounge.length, lounge.length) print(snug.name, snug.height, snug.length, snug.length) print(lounge.name, 'length in feet:', lounge.height_in_ft()) print(f'{snug.name} wall area: {snug.wall_area()} in sq.mm., ' + \ f'{snug.sqmm_to_sqft(snug.wall_area()):.2f} in sq.ft.') Another useful decorator is @property, which allows you to refer to a method as if it is an attribute. Not used in the example, but if I put that before the height_in_ft methods you could say, for example, lounge.height_in_ft instead of lounge.height_in_ft(). One can write classes that are based on other classes. These child classes inherit all of the characteristics of the parent (or super) class but any attribute or method can be overridden to use alternatives that apply only to the child (and its children). Such child classes might have additional methods, alternative __init__ methods, different default output when referenced in a print statement, and so on. The example code code does not demonstrate this feature. More on reddit.com
🌐 r/learnpython
12
2
December 30, 2021
Top answer
1 of 12
2349

Yep, using the staticmethod decorator:

class MyClass(object):
    @staticmethod
    def the_static_method(x):
        print(x)

MyClass.the_static_method(2)  # outputs 2

Note that some code might use the old method of defining a static method, using staticmethod as a function rather than a decorator. This should only be used if you have to support ancient versions of Python (2.2 and 2.3):

class MyClass(object):
    def the_static_method(x):
        print(x)
    the_static_method = staticmethod(the_static_method)

MyClass.the_static_method(2)  # outputs 2

This is entirely identical to the first example (using @staticmethod), just not using the nice decorator syntax.

Finally, use staticmethod sparingly! There are very few situations where static-methods are necessary in Python, and I've seen them used many times where a separate "top-level" function would have been clearer.


The following is verbatim from the documentation::

A static method does not receive an implicit first argument. To declare a static method, use this idiom:

class C:
    @staticmethod
    def f(arg1, arg2, ...): ...

The @staticmethod form is a function decorator – see the description of function definitions in Function definitions for details.

It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class.

Static methods in Python are similar to those found in Java or C++. For a more advanced concept, see classmethod().

For more information on static methods, consult the documentation on the standard type hierarchy in The standard type hierarchy.

New in version 2.2.

Changed in version 2.4: Function decorator syntax added.

2 of 12
261

I think that Steven is actually right. To answer the original question, then, in order to set up a class method, simply assume that the first argument is not going to be a calling instance, and then make sure that you only call the method from the class.

(Note that this answer refers to Python 3.x. In Python 2.x you'll get a TypeError for calling the method on the class itself.)

For example:

class Dog:
    count = 0 # this is a class variable
    dogs = [] # this is a class variable

    def __init__(self, name):
        self.name = name #self.name is an instance variable
        Dog.count += 1
        Dog.dogs.append(name)

    def bark(self, n): # this is an instance method
        print("{} says: {}".format(self.name, "woof! " * n))

    def rollCall(n): #this is implicitly a class method (see comments below)
        print("There are {} dogs.".format(Dog.count))
        if n >= len(Dog.dogs) or n < 0:
            print("They are:")
            for dog in Dog.dogs:
                print("  {}".format(dog))
        else:
            print("The dog indexed at {} is {}.".format(n, Dog.dogs[n]))

fido = Dog("Fido")
fido.bark(3)
Dog.rollCall(-1)
rex = Dog("Rex")
Dog.rollCall(0)

In this code, the "rollCall" method assumes that the first argument is not an instance (as it would be if it were called by an instance instead of a class). As long as "rollCall" is called from the class rather than an instance, the code will work fine. If we try to call "rollCall" from an instance, e.g.:

rex.rollCall(-1)

however, it would cause an exception to be raised because it would send two arguments: itself and -1, and "rollCall" is only defined to accept one argument.

Incidentally, rex.rollCall() would send the correct number of arguments, but would also cause an exception to be raised because now n would be representing a Dog instance (i.e., rex) when the function expects n to be numerical.

This is where the decoration comes in: If we precede the "rollCall" method with

@staticmethod

then, by explicitly stating that the method is static, we can even call it from an instance. Now,

rex.rollCall(-1)

would work. The insertion of @staticmethod before a method definition, then, stops an instance from sending itself as an argument.

You can verify this by trying the following code with and without the @staticmethod line commented out.

class Dog:
    count = 0 # this is a class variable
    dogs = [] # this is a class variable

    def __init__(self, name):
        self.name = name #self.name is an instance variable
        Dog.count += 1
        Dog.dogs.append(name)

    def bark(self, n): # this is an instance method
        print("{} says: {}".format(self.name, "woof! " * n))

    @staticmethod
    def rollCall(n):
        print("There are {} dogs.".format(Dog.count))
        if n >= len(Dog.dogs) or n < 0:
            print("They are:")
            for dog in Dog.dogs:
                print("  {}".format(dog))
        else:
            print("The dog indexed at {} is {}.".format(n, Dog.dogs[n]))


fido = Dog("Fido")
fido.bark(3)
Dog.rollCall(-1)
rex = Dog("Rex")
Dog.rollCall(0)
rex.rollCall(-1)
🌐
W3schools
w3schools.tech › tutorial › python › python_static_methods
Python - Static Methods - Object Oriented Programming - W3schools
By using the @staticmethod decorator or the staticmethod() function, you can easily define and call static methods in your Python classes.
🌐
Tutorial Teacher
tutorialsteacher.com › python › staticmethod-decorator
Python Static Method Decorator - @staticmethod
The static method cannot access the class attributes or the instance attributes. The static method can be called using ClassName.MethodName() and also using object.MethodName(). It can return an object of the class. The following example demonstrates how to define a static method in the class:
🌐
Programiz
programiz.com › python-programming › methods › built-in › staticmethod
Python staticmethod()
The staticmethod() built-in function returns a static method for a given function. In this tutorial, we will learn about Python staticmethod() and its use-case with the help of examples.
🌐
GeeksforGeeks
geeksforgeeks.org › class-method-vs-static-method-python
Class method vs Static method in Python - GeeksforGeeks
July 26, 2024 - In general, static methods know nothing about the class state. They are utility-type methods that take some parameters and work upon those parameters. On the other hand class methods must have class as a parameter.
Find elsewhere
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › functions › staticmethod.html
staticmethod — Python Reference (The Right Way) 0.1 documentation
This means we can put a function inside a class but we can’t access the instance of that class (this is useful when your method does not use the instance). staticmethods can be used when the code that belongs to a class doesn’t use the object itself at all. Python doesn’t have to instantiate a bound method for each object we instantiate.
🌐
GeeksforGeeks
geeksforgeeks.org › python-staticmethod
Python @staticmethod - GeeksforGeeks
November 21, 2019 - When function decorated with @staticmethod is called, we don’t pass an instance of the class to it as it is normally done with methods. It means that the function is put inside the class but it cannot access the instance of that class.
🌐
Real Python
realpython.com › ref › glossary › static-method
static method | Python Glossary – Real Python
In this example, .add() and .subtract() are static methods that perform addition and subtraction, respectively. You can call them directly on the Calculator class or on an instance of the class without affecting instance or class state. ... In this tutorial, you'll compare Python's instance methods, class methods, and static methods.
🌐
Python Basics
pythonbasics.org › home › python basics › python's static methods demystified
Python's Static Methods Demystified - pythonbasics.org
You can however do something else: call a method in a class without creating an object. Demonstration of static method below. Define a class with a method. Add the keyword @staticmethod above it to make it static. ... A class can contain both static and non-static methods. If you want to call ...
🌐
Medium
medium.com › @ryan_forrester_ › class-methods-vs-static-methods-in-python-a-clear-guide-47fcfd385e27
Class Methods vs Static Methods in Python: A Clear Guide | by ryan | Medium
November 4, 2024 - class Example: class_variable = "I'm shared across all instances" def __init__(self, instance_var): self.instance_var = instance_var @classmethod def class_method(cls): print(f"Class method accessing class variable: {cls.class_variable}") return cls("Created via class method") @staticmethod def static_method(): print("Static method can't access class or instance variables directly") return "Static result" def instance_method(self): print(f"Instance method accessing instance var: {self.instance_var}") # Using the methods example = Example("instance value") # Class method can access class state
Top answer
1 of 1
22

Have you already read this?

https://en.wikipedia.org/wiki/Method_(computer_programming)

Especially this?

https://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods

Explanation

In OOP you define classes that you later on instantiate. A class is nothing more than a blueprint: Once you instantiate objects from a class your object will follow exactly the blueprint of your class. That means: If you define a field named "abc" in your class you will later on have a field "abc" in your object. If you define a method "foo()" in your class, you will later on have a method "foo()" to be invoked on your object.

Please note that this "on your object" is essential: You always instantiate a class and then you can invoke the method. This is the "normal" way.

A static method is different. While a normal method always requires to have an instance (where you then can invoke this method at) a static method does not. A static method exists independently from your instances (that's why it is named "static"). So a static method is associated with your class definition itself and therefore is always there and therefore can be invoked only at your class itself. It is completely independent from all instances.

That's a static method.

Python's implementation is a bit ... well ... simple. In details there are deviations from this description above. But that does not make any difference: To be in line with OOP concepts you always should use methods exactly as described above.

Example

Let's give you an example:

class FooBar:

  def someMethod(self):
    print("abc")

This is a regular (instance) method. You use it like this:

myObj = FooBar()
myObj.someMethod()

If you have ...

myObjB = FooBar()
myObjB.someMethod()

... you have an additional instance and therefore invoking someMethod() on this second instance will be the invocation of a second someMethod() method - defined at the second object. This is because you instantiate objects before use so all instances follow the blueprint FooBar defined. All instances therefore receive some kind of copy of someMethod().

(In practice Python will use optimizations internally, so there actually is only one piece of code that implements your someMethod() in memory, but forget about this for now. To a programmer it appears as that every instance of a class will have a copy of the method someMethod(). And that's the level of abstraction that is relevant to us as this is the "surface" we work on. Deep within the implementation of a programming or script language things might be a bit different but this is not very relevant.)

Let's have a look at a static method:

class FooBar:

  @staticmethod
  def someStaticMethod():
    print("abc")

Such static methods can be invoked like this:

FooBar.someStaticMethod()

As you can see: No instance. You directly invoke this method in the context of the class itself. While regular methods work on the particular instance itself - they typically modify data within this instance itself - a class method does not. It could modify static (!) data, but typically it does not anyway.

Consider a static method a special case. It is rarely needed. What you typically want if you write code is not to implement a static method. Only in very specific situations it makes sense to implement a static method.

The self parameter

Please note that a standard "instance" method always must have self as a first argument. This is a python specific. In the real world Python will (of course!) store your method only once in memory, even if you instantiate thousands of objects of your class. Consider this an optimization. If you then invoke your method on one of your thousands of instances, always the same single piece of code is called. But for it to distinguish on which particular object the code of the method should work on your instance is passed to this internally stored piece of code as the very first argument. This is the self argument. It is some kind of implicit argument and always needed for regular (instance) methods. (Not: static methods - there you don't need an instance to invoke them).

As this argument is implicit and always needed most programming languages hide it to the programmer (and handle this internally - under the hood - in the correct way). It does not really make much sense to expose this special argument anyway.

Unfortunately Python does not follow this principle. Python does not hide this argument which is implicitly required. (Python's incorporation of OOP concepts is a bit ... simple.) Therefore you see self almost everywhere in methods. In your mind you can ignore it, but you need to write it explicitly if you define your own classes. (Which is something you should do in order to structure your programs in a good way.)

The static method __new__()

Python is quite special. While regular programming languages follow a strict and immutable concept of how to create instances of particular classes, Python is a bit different here. This behavior can be changed. This behavior is implemented in __new__(). So if you do this ...

myObj = FooBar()

... Python implicitly invokes FooBar.__new__() which in turn invokes a constructor-like (instance) method named __init__() that you could (!) define in your class (as an instance method) and then returns the fully initialized instance. This instance is then what is stored in myObj in this example her.

You could modify this behavior if you want. But this would requires a very very very particularly unusual use case. You will likely never have anything to do with __new__() itself in your entire work with Python. My advice: If you're somehow new to Python just ignore it.

🌐
Medium
medium.com › @rahman.poland.ce21 › what-is-static-method-in-python-explanation-examples-and-advantages-33e4b0916aac
What is the Static Method in Python? Explanation, Examples, and Advantages | by A. Rahman | Medium
January 11, 2025 - Static methods are defined using the @staticmethod decorator before the function declaration. Unlike instance methods, static methods do not accept unique parameters such as self (for instance) or cls (for classes).
🌐
TutorialsPoint
tutorialspoint.com › class-method-vs-static-method-in-python
Class method vs static method in Python
July 30, 2019 - Syntax for Static Method. class my_class: @staticmethod deffunction_name(arguments): #Function Body return value · The Static methods are used to do some utility tasks, and class methods are used for factory methods.
🌐
Python Tutorial
pythontutorial.net › home › python oop › python static methods
Python Static Methods Explained Clearly By Practical Examples
October 25, 2021 - In practice, you use static methods to define utility methods or group functions that have some logical relationships in a class. To define a static method, you use the @staticmethod decorator: class className: @staticmethod def static_method_name(param_list): passCode language: Python (python)
🌐
Real Python
realpython.com › instance-class-and-static-methods-demystified
Python's Instance, Class, and Static Methods Demystified – Real Python
March 17, 2025 - In this tutorial, you'll compare Python's instance methods, class methods, and static methods. You'll gain an understanding of when and how to use each method type to write clear and maintainable object-oriented code.
🌐
Real Python
realpython.com › ref › builtin-functions › staticmethod
staticmethod() | Python’s Built-in Functions – Real Python
In this example, .as_currency() and .as_percent() are static methods that format numbers without needing access to any instance or class variables. This helps to logically group utility functions within the Formatter class. ... In this tutorial, you'll compare Python's instance methods, class methods, and static methods.
🌐
Tech with Tim
techwithtim.net › tutorials › python-programming › classes-objects-in-python › static-and-class-methods
Python Tutorial - Static and Class Methods
When we create a static method we must use something called a decorator. The decorator for a static method is "@staticmethod". class myClass: def __init__(self): self.x = x @staticmethod def staticMethod(): return "i am a static method" # Notice staticMethod does not require the self parameter