我正在自学Python,我最近的一课是Python不是Java,所以我刚刚花了一段时间把我所有的Class方法变成了函数。

我现在意识到,我不需要使用Class方法来做我在Java中使用静态方法所做的事情,但现在我不确定什么时候我会使用它们。我能找到的所有关于Python类方法的建议都是,像我这样的新手应该避开它们,而标准文档在讨论它们时是最不透明的。

谁有一个在Python中使用类方法的好例子,或者至少有人能告诉我什么时候可以合理地使用类方法吗?


当前回答

我也问过自己几次同样的问题。尽管这里的人试图努力解释它,恕我直言,我找到的最好的答案(也是最简单的)答案是Python文档中对Class方法的描述。

还有对静态方法的引用。如果有人已经知道实例方法(我假设是这样),这个答案可能是把它们放在一起的最后一块……

关于这个主题的进一步和更深入的阐述也可以在文档中找到: 标准类型层次结构(向下滚动到实例方法部分)

其他回答

它允许您编写可与任何兼容类一起使用的泛型类方法。

例如:

@classmethod
def get_name(cls):
    print cls.name

class C:
    name = "tester"

C.get_name = get_name

#call it:
C.get_name()

如果你不使用@classmethod,你可以用self关键字来做,但它需要一个Class的实例:

def get_name(self):
    print self.name

class C:
    name = "tester"

C.get_name = get_name

#call it:
C().get_name() #<-note the its an instance of class C

我从Ruby学到的是,所谓的类方法和所谓的实例方法只是一个函数,它的第一个参数具有语义意义,当函数作为对象的方法(即obj.meth())调用时,该参数会被无声地传递。

通常,该对象必须是一个实例,但@classmethod方法装饰器更改规则以传递一个类。你可以在一个实例上调用一个类方法(它只是一个函数)-第一个参数将是它的类。

因为它只是一个函数,所以在任何给定的作用域(即类定义)中只能声明一次。因此,对于ruby开发者来说,你不能让一个类方法和一个实例方法同名。

考虑一下:

class Foo():
  def foo(x):
    print(x)

你可以在实例上调用foo

Foo().foo()
<__main__.Foo instance at 0x7f4dd3e3bc20>

但不是在课堂上:

Foo.foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method foo() must be called with Foo instance as first argument (got nothing instead)

现在添加@classmethod:

class Foo():
  @classmethod
  def foo(x):
    print(x)

调用一个实例现在会通过它的类:

Foo().foo()
__main__.Foo

调用类也一样:

Foo.foo()
__main__.Foo

按照惯例,我们在实例方法中使用self作为第一个参数,在类方法中使用cls。我在这里用这两个都不是来说明它只是一个论点。在Ruby中,self是一个关键字。

与Ruby的对比:

class Foo
  def foo()
    puts "instance method #{self}"
  end
  def self.foo()
    puts "class method #{self}"
  end
end

Foo.foo()
class method Foo

Foo.new.foo()
instance method #<Foo:0x000000020fe018>

Python类方法只是一个装饰函数,您可以使用相同的技术来创建自己的装饰器。修饰过的方法包装了真正的方法(在@classmethod的情况下,它传递了额外的类参数)。底层的方法仍然存在,隐藏但仍然可以访问。


脚注:在类和实例方法之间的名称冲突引起了我的好奇心之后,我写了这篇文章。我不是Python专家,如果有任何错误,我希望得到评论。

替代构造函数是经典的例子。

当然,类定义了一组实例。类的方法作用于单个实例。类方法(和变量)用于将与实例集相关的其他信息挂起。

例如,如果你的类定义了一组学生,你可能想要类变量或方法来定义学生可以成为成员的年级集。

您还可以使用类方法定义用于处理整个集合的工具。例如,Student.all_of_em()可能返回所有已知的学生。显然,如果您的实例集具有比集合更多的结构,您可以提供类方法来了解该结构。Students.all_of_em(等级=“下属”)

这样的技术往往会导致将实例集的成员存储到植根于类变量的数据结构中。这时,您需要注意避免使垃圾收集受挫。

Think about it this way: normal methods are useful to hide the details of dispatch: you can type myobj.foo() without worrying about whether the foo() method is implemented by the myobj object's class or one of its parent classes. Class methods are exactly analogous to this, but with the class object instead: they let you call MyClass.foo() without having to worry about whether foo() is implemented specially by MyClass because it needed its own specialized version, or whether it is letting its parent class handle the call.

当您在创建实际实例之前进行设置或计算时,类方法是必不可少的,因为在实例存在之前,您显然不能将实例用作方法调用的调度点。在SQLAlchemy源代码中可以看到一个很好的例子;在下面的链接中查看dbapi()类方法:

https://github.com/zzzeek/sqlalchemy/blob/ab6946769742602e40fb9ed9dde5f642885d1906/lib/sqlalchemy/dialects/mssql/pymssql.py#L47

You can see that the dbapi() method, which a database backend uses to import the vendor-specific database library it needs on-demand, is a class method because it needs to run before instances of a particular database connection start getting created — but that it cannot be a simple function or static function, because they want it to be able to call other, supporting methods that might similarly need to be written more specifically in subclasses than in their parent class. And if you dispatch to a function or static class, then you "forget" and lose the knowledge about which class is doing the initializing.