Python 类和实例方法的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17134653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Class and Instance methods
提问by James
I was reading PEP 0008 (Style Guide), and I noticed that it suggested to use self as the first argument in an instance method, but cls as the first argument in a class method.
我正在阅读 PEP 0008(样式指南),我注意到它建议使用 self 作为实例方法中的第一个参数,而将 cls 作为类方法中的第一个参数。
I've used and written a few classes, but I've never encountered a class method (Well, a method which passes cls as a parameter). Could anybody show me some examples?
我已经使用并编写了一些类,但我从未遇到过类方法(嗯,一种将 cls 作为参数传递的方法)。有人可以给我看一些例子吗?
Thanks!
谢谢!
采纳答案by James
Instance methods
实例方法
When creating an instance method, the first parameter is always self.
You can name it anything you want, but the meaning will always be the same, and you should use selfsince it's the naming convention.
selfis (usually) passed hiddenly when calling an instance method; it represents the instance calling the method.
创建实例方法时,第一个参数始终是self。您可以随意命名它,但含义始终相同,您应该使用self它,因为这是命名约定。
self(通常)在调用实例方法时隐藏传递;它代表调用该方法的实例。
Here's an example of a class called Instthat has an instance method called introduce():
这是一个名为的类的示例,该类Inst具有一个名为 的实例方法introduce():
class Inst:
def __init__(self, name):
self.name = name
def introduce(self):
print("Hello, I am %s, and my name is " %(self, self.name))
Now to call this method, we first need to create an instance of our class.
Once we have an instance, we can call introduce()on it, and the instance will automatically be passed as self:
现在要调用这个方法,我们首先需要创建我们类的一个实例。一旦我们有了一个实例,我们就可以调用introduce()它,该实例将自动传递为self:
myinst = Inst("Test Instance")
otherinst = Inst("An other instance")
myinst.introduce()
# outputs: Hello, I am <Inst object at x>, and my name is Test Instance
otherinst.introduce()
# outputs: Hello, I am <Inst object at y>, and my name is An other instance
As you see, we're not passing the parameter self, it get's hiddenly passed with the period operator; we're calling Instclass's instance method introduce, with the parameter of myinstor otherinst.
This means that we can call Inst.introduce(myinst)and get the exact same result.
如您所见,我们没有传递参数self,而是通过句点运算符隐藏地传递了它;我们正在调用Inst类的实例方法introduce,参数为myinstor otherinst。这意味着我们可以调用Inst.introduce(myinst)并获得完全相同的结果。
Class methods
类方法
The idea of class method is very similar to instance method, only difference being that instead of passing the instance hiddenly as a first parameter, we're now passing the class itself as a first parameter.
类方法的思想与实例方法非常相似,唯一的区别是我们现在将类本身作为第一个参数传递,而不是将实例作为第一个参数隐藏地传递。
class Cls:
@classmethod
def introduce(cls):
print("Hello, I am %s!" %cls)
Since we're passing only a class to the method, no instance is involved. This means that we don't need an instance at all, we call the class method as if it was a static function:
由于我们只将一个类传递给该方法,因此不涉及任何实例。这意味着我们根本不需要实例,我们像调用静态函数一样调用类方法:
Cls.introduce() # same as Cls.introduce(Cls)
# outputs: Hello, I am <class 'Cls'>
Notice that again Clsis passed hiddenly, so we could also say Cls.introduce(Inst)and get output "Hello, I am <class 'Inst'>.
This is particularly useful when we're inheriting a class from Cls:
请注意,再次Cls隐藏传递,因此我们也可以说Cls.introduce(Inst)并获得输出"Hello, I am <class 'Inst'>。当我们从Cls以下继承一个类时,这特别有用:
class SubCls(Cls):
pass
SubCls.introduce()
# outputs: Hello, I am <class 'SubCls'>

