Python种staticmethod和classmethod有什么区别,具体用法呢。什么时候用。
黄舟
黄舟 2017-04-17 16:25:00
0
6
419
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(6)
迷茫

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

# -*- 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()
Ty80

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.

巴扎黑
Python2.7(Python3中与这个有点不同)
class A(object):
    @staticmethod
    def staticMethod():
        return 'static method'
    @classmethod
    def classMethod(cls):  #此处的cls必须要,某种形式上的吧(见后面)
        #cls到底是什么,测试一下
        print cls
        return 'class method'
        
a = A()
a.staticMehtod()  #ok
A.staticMehtod() #ok
a.classMethod()  #ok
A.classMethod()  #ok

[总结]:
classmethod就是类方法,跟着类走(不是实例),staticmethod类似于恰好放在类中的一个局部函数(不是方法),非常像类方法
staticmethod处理一个类的本地数据,classmethod处理类层级中属于类的数据
[总结]
好像没有什么很大的不同,楼主看感觉用吧,语法别搞错了.
另外在Python3中,这些东西就好多了,没有2中这么多唧唧歪歪的东西.
阿神

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通常意味着被修饰的方法或者变量不属于实例,而是属于类。

静态方法的作用在于:如何一个方法需要在不构造实例的情况下调用,那么这个方法就需要被定义成静态。静态方法不能被override

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. 🎜
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!