我可以定义一个static method,我可以直接在类实例上调用?
MyClass.the_static_method()

是的,使用staticmethod
装饰器:
class MyClass(object):
@staticmethod
def the_static_method(x):
print(x)
MyClass.the_static_method(2) # outputs 2
请注意,某些代码可能使用定义静态方法的旧方法,使用staticmethod
作为函数而不是装饰器。这仅在您必须支持古代版本的 Python(2.2 和 2.3)时才应使用:
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
),只是没有使用漂亮的装饰器语法。
最后,谨慎使用staticmethod
!在 Python 中,静态方法是必需的情况很少,我已经看到它们多次使用,其中单独的“顶级”函数会更清晰。
静态方法不接收隐式的第一个参数。要声明静态方法,请使用以下习语:
class C:
@staticmethod
def f(arg1, arg2, ...): ...
@ staticmethod 形式是一个函数decorator-有关详细信息,请参见Function definitions中的函数定义说明。
它可以在类(如C.f()
)或实例(如C().f()
)上调用。
Python 中的静态方法类似于 Java 或 C++ 中的静态方法。有关更高级的概念,请参阅classmethod()
。
有关静态方法的更多信息,请参考The standard type hierarchy中关于标准类型层次结构的文档。
新版本 2.2。
在版本 2.4 中更改:添加了函数装饰器语法。
我认为Steven is actually right。要回答原始问题,然后,为了设置一个类方法,只需假设第一个参数不会是一个调用实例,然后确保你只从类中调用该方法。
(请注意,这个答案是指 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 为数字时,现在 n 将表示 Dog 实例(即 rex)。
这就是装饰的地方:如果我们在“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 C:
... @staticmethod
... def hello():
... print "Hello World"
...
>>> C.hello()
Hello World

你真的不需要使用@staticmethod
装饰器。只是声明一个方法 (不期望 self 参数) 并从类中调用它。装饰器仅在您希望能够从实例调用它的情况下存在 (这不是您想要做的)
大多数情况下,你只是使用功能虽然...
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(44条)