Python类中的静态方法是什么?
任何python类都具有三种类型的方法,例如实例方法,类方法和静态方法。
示例
考虑代码
class OurClass:
def method(self):
return 'instance method called', self
@classmethod
def classmethod(cls):
return 'class method called', cls
@staticmethod
def staticmethod():
return 'static method called'第三个方法OurClass.staticmethod用@staticmethod装饰器标记,以将其标记为静态方法。
这种方法既不使用self也不使用cls参数,但可以接受任意数量的其他参数。
因此,静态方法无法修改对象状态或类状态。静态方法在可以访问哪些数据方面受到限制-它们主要是为方法命名空间的一种方法。我们可以从上面的代码中调用staticmethod,如下所示
>>> obj = OurClass()>>> obj.staticmethod() 'static method called'
热门推荐
10 最美婆婆生日祝福语简短
11 新年祝福语简短给孩子
12 年前幸福祝福语大全简短
13 中考祝福语简短卡片大全
14 蛋糕祝老师祝福语简短
15 舞蹈老师晚安祝福语简短
16 祝福语诗意文案简短
17 祖国七十华诞简短祝福语
18 八一幼儿祝福语大全简短