問題描述
可能重復:
Python自我"解釋
我正在學習 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++ 這樣的語言可以讓你把 this->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.
使用顯式引用,不需要特殊的語法對于方法定義也不必擔心復雜關于變量查找的語義.相反,一個簡單的定義第一個參數對應于實例的函數,通過約定被命名為自我".例如:
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 中看到的方法,它具有已經為我提供了導入和異常處理的語法.Modula-3 沒有類,但它允許您創建記錄類型包含已初始化的全類型函數指針成員默認為附近定義的函數,并添加語法糖,所以如果 x 是這樣一個記錄變量,而 m 是一個函數指針該記錄的成員,初始化為函數 f,然后調用x.m(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 FAQ中也有答案.
There is also an answer in the Python FAQ.
這篇關于為什么從類中訪問類變量需要“self".在 Python 中?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!