为什么从类中访问类变量需要“self”。在Python? [英] Why accessing to class variable from within the class needs "self." in Python?

查看:182
本文介绍了为什么从类中访问类变量需要“self”。在Python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Python'self'已解释

我正在学习Python,问题,更理论上比实际,关于访问类变量从这个类的方法。

I'm learning Python and I have a question, more theoretical than practical, regarding access class variables from method of this class.

例如我们有:

class ExampleClass:
    x = 123
    def example_method(self):
        print(self.x)

为什么必须写 self.x ,而不只是 x x 属于类的命名空间,使用它的方法也属于它。我缺少什么?

Why is necessarily to write exactly self.x, not just x? x belongs to namespace of the class, and method using it belongs to it too. What am I missing? What a rationale stands behind such style?

在C ++中,你可以这样写:

In C++ you can write:

class ExampleClass {
public:
    int x;
    void example_method()
    {
        x = 123;
        cout << x;
    };
};

它会奏效!

推荐答案

Python的历史:添加对用户定义类的支持


相反,我决定放弃隐式引用
实例变量。类似C ++的语言让你写这个 - > foo到
显式引用实例变量foo(如果有一个
单独的局部变量foo)。因此,我决定使这种明确的
引用唯一的方式来引用实例变量。此外,
我决定,而不是使当前对象(t​​his)一个
特殊关键字,我会简单地使this(或其等效的)
第一命名参数一个方法。实例变量总是
被引用作为该参数的属性。

Instead, I decided to give up on the idea of implicit references to instance variables. Languages like C++ let you write this->foo to explicitly reference the instance variable foo (in case there’s a separate local variable foo). Thus, I decided to make such explicit references the only way to reference instance variables. In addition, I decided that rather than making the current object ("this") a special keyword, I would simply make "this" (or its equivalent) the first named argument to a method. Instance variables would just always be referenced as attributes of that argument.

对于显式引用,没有必要有一个特殊的语法
方法定义,也不必担心有关变量查找的复杂
语义。相反,只需定义一个
函数,其第一个参数对应于实例,其中
约定名为self。例如:

With explicit references, there is no need to have a special syntax for method definitions nor do you have to worry about complicated semantics concerning variable lookup. Instead, one simply defines a function whose first argument corresponds to the instance, which by convention is named "self." For example:

def spam(self,y):
    print self.x, y

这种方法类似于我在Modula-3中看到的,它有
已经提供了import语法和异常处理。
Modula-3没有类,但它允许您创建记录类型
包含完全类型化的函数指针成员,这些成员被初始化为
,默认为附近定义的函数,并添加语法糖类
如果x是这样的记录变量,并且m是该记录的函数指针
成员,初始化为函数f,则调用
xm(args)等效于调用f(x, args)。这匹配
典型的对象和方法的实现,并使得
可以将实例变量与第一个参数的属性等同。

This approach resembles something I had seen in Modula-3, which had already provided me with the syntax for import and exception handling. Modula-3 doesn’t have classes, but it lets you create record types containing fully typed function pointer members that are initialized by default to functions defined nearby, and adds syntactic sugar so that if x is such a record variable, and m is a function pointer member of that record, initialized to function f, then calling x.m(args) is equivalent to calling f(x, args). This matches the typical implementation of objects and methods, and makes it possible to equate instance variables with attributes of the first argument.

所以,BDFL自己指出,他决定使用显式自我隐含自我的唯一真正原因是:

So, stated by the BDFL himself, the only real reason he decided to use explicit self over implicit self is that:


  • 它是显式的

  • 它更容易实现,因为查找必须在运行时完成(而不是像其他语言一样在编译时),并且具有隐式self可能会增加复杂性

编辑:Python中还有一个答案常见问题

There is also an answer in the Python FAQ.

这篇关于为什么从类中访问类变量需要“self”。在Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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