类和实例方法的区别 [英] Difference between Class and Instance methods

查看:53
本文介绍了类和实例方法的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 PEP 0008(风格指南),我注意到它建议使用self 作为实例方法中的第一个参数,而 cls 作为类方法中的第一个参数.

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.

我已经使用并编写了一些类,但我从未遇到过类方法(嗯,一种将 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?

谢谢!

推荐答案

实例方法

创建实例方法时,第一个参数总是self.您可以随意命名,但含义始终相同,您应该使用 self,因为这是命名约定.self 在调用实例方法时(通常)是隐藏传递的;它代表调用该方法的实例.

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 self since it's the naming convention. self is (usually) passed hiddenly when calling an instance method; it represents the instance calling the method.

这是一个名为 Inst 的类的示例,它有一个名为 introduce() 的实例方法:

Here's an example of a class called Inst that has an instance method called 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))

现在要调用这个方法,我们首先需要创建我们类的一个实例.一旦我们有了一个实例,我们就可以在它上面调用introduce(),这个实例会自动作为self传递:

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:

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

如您所见,我们没有传递参数 self,它是通过句点运算符隐藏地传递的;我们正在调用Inst 类的实例方法introduce,参数为myinstotherinst.这意味着我们可以调用 Inst.introduce(myinst) 并获得完全相同的结果.

As you see, we're not passing the parameter self, it get's hiddenly passed with the period operator; we're calling Inst class's instance method introduce, with the parameter of myinst or otherinst. This means that we can call Inst.introduce(myinst) and get the exact same result.

类方法的思想与实例方法非常相似,唯一不同的是,我们现在将类本身作为第一个参数传递,而不是将实例作为第一个参数隐藏地传递.

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'>

注意再次Cls是隐藏传递的,所以我们也可以说Cls.introduce(Inst)并得到输出"Hello, I am <类Inst">.当我们从 Cls 继承一个类时,这特别有用:

Notice that again Cls is 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:

class SubCls(Cls):
    pass

SubCls.introduce()
# outputs: Hello, I am <class 'SubCls'>

这篇关于类和实例方法的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆