[英]Why Use Class Methods Instead Of Just Using The Class Name For Instance?
I have a question. 我有个问题。 I am watching class tutorials and cam across class methods. 我正在看课程教程和跨类方法的摄像头。 My question can be better understood after seeing the code. 看完代码,可以更好地理解我的问题。 I will show the code first. 我将首先显示代码。
The Code: 编码:
class Person:
def __init__(self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
self.from_lastname()
def from_lastname(self):
Person.mostpreviouslastname = self.lastname
p1 = Person('Mike', 'Wizoski')
print(Person.mostpreviouslastname)
print(p1.lastname)
p2 = Person ('Indiana', 'Jones')
print(p1.lastname)
print(p2.lastname)
print(Person.mostpreviouslastname)
That was the part without class methods. 那是没有类方法的部分。 This is with: 这是与:
class Person:
def __init__(self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
Person.from_lastname(self.lastname)
@classmethod
def from_lastname(cls, last_name1):
cls.mostpreviouslastname = last_name1
p1 = Person('Mike', 'Wizoski')
print(Person.mostpreviouslastname)
print(p1.lastname)
p2 = Person ('Indiana', 'Jones')
print(p1.lastname)
print(p2.lastname)
print(Person.mostpreviouslastname)
They both work the same. 他们两个都一样。 So my question is why even use class methods at all? 所以我的问题是,为什么还要使用类方法呢?
Classmethods in python make more sense when you consider inheritance. 考虑继承时,python中的类方法更有意义。 Let's use a slightly more evocative example: 让我们使用一个更令人回味的示例:
Say you have a class Item
that represents an item in your grocery store. 假设您有一个类Item
,它代表杂货店中的某个项目。 Let's also say that all Item
s can come in a two-pack. 我们还可以说所有Item
都可以装成两包。 We'll represent that with a classmethod that returns a tuple. 我们将使用返回元组的类方法来表示它。
class Item:
@classmethod
def two_pack(cls):
return cls(), cls()
Now let's say you want to subclass Item
to really represent specific items, like Soda
现在假设您要对Item
进行子类化以真正代表特定的项目,例如Soda
class Soda(Item):
pass
Now we can call Soda.two_pack()
and get a tuple of Soda
objects, even though the two_pack
method doesn't know about Soda
objects at all. 现在,我们可以调用Soda.two_pack()
并获取一个元组的Soda
对象,即使two_pack
方法根本不了解Soda
对象。
This also touches on the main use of class methods, which is providing alternate means of building an object, so you're not limited to what firs in __init__
这也触及了类方法的主要用法,类方法提供了构建对象的替代方法,因此您不仅限于__init__
中的__init__
In your example you likely wouldn't want to use class methods. 在您的例子中,你可能不希望使用类的方法。 That's because there can only be one value of fullname
and it's attached to the Person
class object. 这是因为fullname
只能有一个值,并且它附加在Person
类对象上。 So if you ever had more than one instance of Person
, then they couldn't have separate fullname
values. 因此,如果您有多个Person
实例 ,则它们不能有单独的fullname
值。
In your case you'd want to remove the class method and have from_fullname
reference self
, so that the names are different for each Person
instance: 在您的情况下,您要删除类方法并使用from_fullname
引用self
,以便每个Person
实例的名称都不同:
class Person:
def __init__(self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
self.from_fullname()
def from_fullname(self):
self.fullname = self.lastname
p1 = Person('Mike', 'Wizoski')
p2 = Person('Bob', 'Jones')
print(p1.fullname)
print(p2.fullname)
# Wizoski
# Jones
If you stuck with either of your examples, you'd get the wrong result when instantiating more than one person: 如果您坚持使用任何一个示例,则在实例化多个人时会得到错误的结果:
# Jones
# Jones
So when would you use class methods? 那么什么时候使用类方法呢? When you have some function that doesn't use any of the instance's data (ie doesn't need a self
reference). 当您有一些不使用实例数据的函数(即不需要self
引用)时。 More detailed explanations can be found here: 可以在这里找到更详细的解释:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.