staticmethod is not declared because you don’t want to create an instance, but it is declared that the method will not change the data of the instance itself.
classmethod is not declared because you don’t want to create an instance, but to operate the class itself (you can operate its own properties and methods after passing in cls).
This is just a superficial personal opinion for reference by the topic owner.
Intuitively, the function signatures of classmethod and staticmethod are different. One has parameters and the other has no parameters. They all belong to python decorators. Note that in classmethod, the parameter does not have to be cls, it can be any named variable. When parent and child classes are not involved, the behavior of the two seems to be the same, but if the parent and child classes are designed, classmethod can determine the calling child class object
# -*- coding: utf-8 -*-
class Parent(object):
@staticmethod
def staticSayHello():
print "Parent static"
@classmethod
def classSayHello(anything): #这里是anything
if anything == Boy:
print "Boy classSayHello"
elif anything == Girl:
print "girl sayHello"
class Boy(Parent):
pass
class Girl(Parent):
pass
if __name__ == '__main__':
Boy.classSayHello()
Girl.classSayHello()
The difference between static, class and ordinary internal methods is not only about subtle differences in usage, but also about code isolation. static does not require any class or instance information. class requires class information. Normal methods require instance information. Don't pay too much attention to these details. When you don't understand, just write it in a normal way. You'll know which one to switch to when you feel the need.
In an object-oriented object language, static usually means that the modified method or variable does not belong to the instance, but to the class. static通常意味着被修饰的方法或者变量不属于实例,而是属于类。
The role of static methods is: if a method needs to be called without constructing an instance, then this method needs to be defined as static. Static methods cannot be override and do not have most OO features. Static methods actually have the idea of a procedural language.
According to my own experience in writing Python code, object-oriented is a bit awkward to use in Python. For example, I don’t know which one to choose between static methods and functions. Therefore, it is recommended to focus on learning Python's own unique features instead of OO. 🎜
staticmethod is not declared because you don’t want to create an instance, but it is declared that the method will not change the data of the instance itself.
classmethod is not declared because you don’t want to create an instance, but to operate the class itself (you can operate its own properties and methods after passing in cls).
This is just a superficial personal opinion for reference by the topic owner.
A little simple understanding
may be useful
A brief chat about @staticmethod and @classmethod in Python class
Intuitively, the function signatures of classmethod and staticmethod are different. One has parameters and the other has no parameters.
They all belong to python decorators. Note that in classmethod, the parameter does not have to be cls, it can be any named variable. When parent and child classes are not involved, the behavior of the two seems to be the same, but if the parent and child classes are designed, classmethod can determine the calling child class object
The difference between static, class and ordinary internal methods is not only about subtle differences in usage, but also about code isolation. static does not require any class or instance information. class requires class information. Normal methods require instance information. Don't pay too much attention to these details. When you don't understand, just write it in a normal way. You'll know which one to switch to when you feel the need.
In an object-oriented object language,
static
usually means that the modified method or variable does not belong to the instance, but to the class.static
通常意味着被修饰的方法或者变量不属于实例,而是属于类。静态方法的作用在于:如何一个方法需要在不构造实例的情况下调用,那么这个方法就需要被定义成静态。静态方法不能被
The role of static methods is: if a method needs to be called without constructing an instance, then this method needs to be defined as static. Static methods cannot beoverride
override
and do not have most OO features. Static methods actually have the idea of a procedural language.