Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we use @staticmethod?

Tags:

python

I just can't see why do we need to use @staticmethod. Let's start with an exmaple.

class test1:
    def __init__(self,value):
        self.value=value

    @staticmethod
    def static_add_one(value):
        return value+1

    @property
    def new_val(self):
        self.value=self.static_add_one(self.value)
        return self.value


a=test1(3)
print(a.new_val) ## >>> 4



class test2:
    def __init__(self,value):
        self.value=value

    def static_add_one(self,value):
        return value+1

    @property
    def new_val(self):
        self.value=self.static_add_one(self.value)
        return self.value


b=test2(3)
print(b.new_val) ## >>> 4

In the example above, the method, static_add_one , in the two classes do not require the instance of the class(self) in calculation.

The method static_add_one in the class test1 is decorated by @staticmethod and work properly.

But at the same time, the method static_add_one in the class test2 which has no @staticmethod decoration also works properly by using a trick that provides a self in the argument but doesn't use it at all.

So what is the benefit of using @staticmethod? Does it improve the performance? Or is it just due to the zen of python which states that "Explicit is better than implicit"?

like image 969
Ken T Avatar asked May 07 '14 03:05

Ken T


People also ask

Why should I use Staticmethod?

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. Bound methods are objects too, and creating them has a cost. Having a static method avoids that.

What is the purpose of Staticmethod in Python?

Static methods are used when we don't want subclasses of a class change/override a specific implementation of a method.

What does Staticmethod mean?

A static method (or static function) is a method defined as a member of an object but is accessible directly from an API object's constructor, rather than from an object instance created via the constructor.

What is the purpose of Classmethod and Staticmethod in Python?

The Static methods are used to do some utility tasks, and class methods are used for factory methods. The factory methods can return class objects for different use cases.


1 Answers

The reason to use staticmethod is if you have something that could be written as a standalone function (not part of any class), but you want to keep it within the class because it's somehow semantically related to the class. (For instance, it could be a function that doesn't require any information from the class, but whose behavior is specific to the class, so that subclasses might want to override it.) In many cases, it could make just as much sense to write something as a standalone function instead of a staticmethod.

Your example isn't really the same. A key difference is that, even though you don't use self, you still need an instance to call static_add_one --- you can't call it directly on the class with test2.static_add_one(1). So there is a genuine difference in behavior there. The most serious "rival" to a staticmethod isn't a regular method that ignores self, but a standalone function.

like image 178
BrenBarn Avatar answered Sep 29 '22 17:09

BrenBarn