Python中的静态方法?

2022-09-05 00:49:58

我可以定义一个可以直接在类实例上调用的静态方法吗?例如,

MyClass.the_static_method()

答案 1

是的,使用静态方法装饰器:

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

这与第一个示例(using )完全相同,只是没有使用漂亮的装饰器语法。@staticmethod

最后,请谨慎使用静态方法!在Python中,静态方法很少是必需的,我已经看到它们被多次使用,其中单独的“顶级”函数会更清晰。


以下是文档中的逐字记录:

静态方法不接收隐式的第一个参数。若要声明静态方法,请使用以下成语:

class C:
    @staticmethod
    def f(arg1, arg2, ...): ...

@staticmethod窗体是函数修饰器 – 有关详细信息,请参阅函数定义中的函数定义说明。

可以在类(如 )或实例(如 )上调用它。该实例将被忽略,但其类除外。C.f()C().f()

Python中的静态方法类似于Java或C++中的静态方法。有关更高级的概念,请参阅 classmethod()。

有关静态方法的详细信息,请参阅标准类型层次结构中有关标准类型层次结构的文档

版本 2.2 中的新功能。

版本2.4中已更改:添加了函数装饰器语法。


答案 2

我认为史蒂文实际上是对的。为了回答原始问题,然后,为了设置类方法,只需假设第一个参数不会是调用实例,然后确保仅从类中调用该方法。

(请注意,这个答案指的是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)