Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we really need @staticmethod decorator in python to declare static method

Tags:

python

static

I am curious about why we need the @staticmethod decorator to declare method as static. I was reading about static methods in Python, and I came to know that static method can be callable without instantiating its class. So I tried the two examples below, but both do the same:

class StatMethod:   def stat():     print("without Decorator")  class StatMethod_with_decorator:   @staticmethod   def stat():     print("With Decorator") 

If I call the stat() method on the class directly, both print/show the values below:

>> StatMethod.stat() without Decorator >> StatMethod_with_decorator.stat() With Decorator 
like image 434
Sanjay Avatar asked Apr 24 '17 11:04

Sanjay


People also ask

Is @staticmethod necessary in Python?

Having a static method avoids that. There are very few situations where static-methods are necessary in Python. The @staticmethod form is a function decorator. Also see classmethod() for a variant that is useful for creating alternate class constructors.

What is the decorator used to declare a static method in Python?

We use @classmethod decorator in python to create a class method and we use @staticmethod decorator to create a static method in python.

What is the decorator used to declare a static method?

The @staticmethod is a built-in decorator that defines a static method in the class in Python.

What is the benefit of the @staticmethod decorator?

This decorator exists so you can create class methods that are passed the actual class object within the function call, much like self is passed to any other ordinary instance method in a class. This follows the static factory pattern very well, encapsulating the parsing logic inside of the method itself.


2 Answers

You need the decorator if you intend to try to call the @staticmethod from the instance of the class instead of of the class directly

class Foo():     def bar(x):         return x + 5  >>> f = Foo() >>> f.bar(4) Traceback (most recent call last):   File "<pyshell#7>", line 1, in <module>     f.bar(4) TypeError: bar() takes 1 positional argument but 2 were given 

Now if I declare @staticmethod the self argument isn't passed implicitly as the first argument

class Foo():     @staticmethod     def bar(x):         return x + 5  >>> f = Foo() >>> f.bar(4) 9 
like image 74
Cory Kramer Avatar answered Oct 05 '22 10:10

Cory Kramer


The documentation describes some transformations that are done when calling a user defined method:

Note that the transformation from function object to (unbound or bound) method object happens each time the attribute is retrieved from the class or instance. In some cases, a fruitful optimization is to assign the attribute to a local variable and call that local variable. Also notice that this transformation only happens for user-defined functions; other callable objects (and all non-callable objects) are retrieved without transformation. It is also important to note that user-defined functions which are attributes of a class instance are not converted to bound methods; this only happens when the function is an attribute of the class.

For methods marked as staticmethod this is different:

Static method objects provide a way of defeating the transformation of function objects to method objects described above. A static method object is a wrapper around any other object, usually a user-defined method object. When a static method object is retrieved from a class or a class instance, the object actually returned is the wrapped object, which is not subject to any further transformation. Static method objects are not themselves callable, although the objects they wrap usually are. Static method objects are created by the built-in staticmethod() constructor.

like image 31
Flurin Avatar answered Oct 05 '22 09:10

Flurin