Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: When to use static method over class method?

Based on what I read, class methods are largely the same as static methods with a few exceptions but have the advantage of providing a class pointer.

As a result, is there really any reason to use static methods over class methods if a non-instance method is defined within a class?

Edit: Since some of you are quick to dismiss this as a duplicate to another question. This is not a question on the difference between the class and static methods. Rather, it is a question on how to decide between the two in the vast majority of cases when their functionality overlap.

Edit #2: The reason I ask is that I am refactoring some existing code from other people. Specifically, there are child classes that share the same modules as the parent and I intend to move them to separate modules. When that occurs, references to out-of-class constants within static methods need to be fixed. I can accomplish that with one of the following ways 1. Import all the constants from the parent module 2. Move all the constants to within parent class and change all the child static methods to class methods 3. Add the "ParentClass." before each reference to the constants

I personally want to do #2 because it avoids namespace contamination and this is also why I asked this question. It's a question of style mostly. Hope this provides enough context.

like image 527
user1836155 Avatar asked Apr 13 '15 20:04

user1836155


People also ask

When should I use static method in Python?

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

When should static methods be used in class?

Since static methods cannot reference instance member variables, they are a good choice for methods that don't require any object state manipulation. When we use static methods for operations where the state is not managed, then method calling is more practical.

What is the difference between a class method and a static method?

Class methods don't need a class instance. They can't access the instance ( self ) but they have access to the class itself via cls . Static methods don't have access to cls or self . They work like regular functions but belong to the class's namespace.

Are static methods faster in Python?

1 Answer. Save this answer. Show activity on this post. It looks like staticmethod is slightly faster (likely just because it doesn't need to pass an argument into the function at all), but we're talking about a difference of 3 milliseconds for 100,000 calls, which is nanoseconds per call in cost.


1 Answers

Almost always, actually. You rarely need access to the class object that is passed automatically to a class method. Python class methods (which should not be compared to class methods in a language like Java) are primarily intended to be used to provide alternate constructors.

Specifically, a class method would look something like

@classmethod
def my_class_method(cls, foo):
   ...

If you never actually use cls in the body of the method, you might first change it to

@staticmethod
def my_static_method(foo):
   ...

Next, you might consider whether my_static_method actually needs to be part of the class and whether it could be a standalone function instead.

If you do use cls, then obviously it needs to be a class method.

like image 81
chepner Avatar answered Oct 20 '22 11:10

chepner