我可以定义一个可以直接在类实例上调用的静态方法吗?例如,
MyClass.the_static_method()
网友回答:
我认为史蒂文实际上是对的。为了回答原始问题,为了设置类方法,只需假设第一个参数不会是调用实例,然后确保仅从类调用该方法。
(请注意,这个答案指的是Python 3.x。在 Python 2.x 中,你会得到一个用于在类本身上调用方法的方法。TypeError
例如:
class Dog:
count = 0 # this is a class variable
dogs = [] # this is a class variable
def __init__(self, name):
self.name = name #self.name is an instance variable
Dog.count += 1
Dog.dogs.append(name)
def bark(self, n): # this is an instance method
print("{} says: {}".format(self.name, "woof! " * n))
def rollCall(n): #this is implicitly a class method (see comments below)
print("There are {} dogs.".format(Dog.count))
if n >= len(Dog.dogs) or n < 0:
print("They are:")
for dog in Dog.dogs:
print(" {}".format(dog))
else:
print("The dog indexed at {} is {}.".format(n, Dog.dogs[n]))
fido = Dog("Fido")
fido.bark(3)
Dog.rollCall(-1)
rex = Dog("Rex")
Dog.rollCall(0)
在此代码中,“rollCall”方法假定第一个参数不是实例(如果它是由实例而不是类调用的)。只要从类而不是实例调用“rollCall”,代码就可以正常工作。如果我们尝试从实例调用“rollCall”,例如:
rex.rollCall(-1)
但是,它会导致引发异常,因为它会发送两个参数:自身和 -1,并且“rollCall”仅定义为接受一个参数。
顺便说一下,rex.rollCall() 会发送正确数量的参数,但也会导致引发异常,因为现在 n 将表示一个 Dog 实例(即 rex),而函数期望 n 是数字。
这就是装饰的用武之地:
如果我们在“rollCall”方法之前使用
@staticmethod
然后,通过显式声明该方法是静态的,我们甚至可以从实例调用它。现在
rex.rollCall(-1)
会工作。然后,在方法定义之前插入@staticmethod会阻止实例将自身作为参数发送。
您可以通过尝试以下代码来验证这一点,无论是否注释掉@staticmethod行。
class Dog:
count = 0 # this is a class variable
dogs = [] # this is a class variable
def __init__(self, name):
self.name = name #self.name is an instance variable
Dog.count += 1
Dog.dogs.append(name)
def bark(self, n): # this is an instance method
print("{} says: {}".format(self.name, "woof! " * n))
@staticmethod
def rollCall(n):
print("There are {} dogs.".format(Dog.count))
if n >= len(Dog.dogs) or n < 0:
print("They are:")
for dog in Dog.dogs:
print(" {}".format(dog))
else:
print("The dog indexed at {} is {}.".format(n, Dog.dogs[n]))
fido = Dog("Fido")
fido.bark(3)
Dog.rollCall(-1)
rex = Dog("Rex")
Dog.rollCall(0)
rex.rollCall(-1)
网友回答:
是的,使用装饰器:staticmethod
class MyClass(object):
@staticmethod
def the_static_method(x):
print(x)
MyClass.the_static_method(2) # outputs 2
请注意,某些代码可能使用定义静态方法的旧方法,用作函数而不是装饰器。仅当您必须支持古代版本的 Python (2.2 和 2.3)时,才应使用此方法:staticmethod
class MyClass(object):
def the_static_method(x):
print(x)
the_static_method = staticmethod(the_static_method)
MyClass.the_static_method(2) # outputs 2
这与第一个示例(使用 )完全相同,只是没有使用漂亮的装饰器语法。@staticmethod
最后,谨慎使用!在 Python 中很少有需要静态方法的情况,我已经看到它们多次使用,其中单独的“顶级”函数会更清晰。staticmethod
以下是文档中的逐字记录:
静态方法不会接收隐式第一个参数。若要声明静态方法,请使用以下习惯用法:
class C: @staticmethod def f(arg1, arg2, ...): ...
@staticmethod窗体是一个函数装饰器 – 有关详细信息,请参阅函数定义中的函数定义说明。
它可以在类(如 )或实例(如 )上调用。实例被忽略,但它的类除外。
C.f()
C().f()
Python 中的静态方法类似于 Java 或 C++ 中的静态方法。有关更高级的概念,请参见。
classmethod()
有关静态方法的详细信息,请参阅标准类型层次结构中有关标准类型层次结构的文档。
版本 2.2 中的新功能。
在 2.4 版更改: 添加了函数装饰器语法。
网友回答:
是的,请查看静态方法装饰器:
>>> class C:
... @staticmethod
... def hello():
... print "Hello World"
...
>>> C.hello()
Hello World
模板简介:该模板名称为【我可以定义一个可以直接在类实例上调用的静态方法吗?】,大小是暂无信息,文档格式为.编程语言,推荐使用Sublime/Dreamweaver/HBuilder打开,作品中的图片,文字等数据均可修改,图片请在作品中选中图片替换即可,文字修改直接点击文字修改即可,您也可以新增或修改作品中的内容,该模板来自用户分享,如有侵权行为请联系网站客服处理。欢迎来懒人模板【Python】栏目查找您需要的精美模板。