爲何Python定義的class中的函數須要self做爲第一個參數

Why must ‘self’ be used explicitly in method definitions and calls?

  The idea was borrowed from Modula-3. It turns out to be very useful, for a variety of reasons.html

  First, it’s more obvious that you are using a method or instance attribute instead of a local variable. Reading self.x or self.meth() makes it absolutely clear that an instance variable or method is used even if you don’t know the class definition by heart. In C++, you can sort of tell by the lack of a local variable declaration (assuming globals are rare or easily recognizable) – but in Python, there are no local variable declarations, so you’d have to look up the class definition to be sure. Some C++ and Java coding standards call for instance attributes to have an m_ prefix, so this explicitness is still useful in those languages, too.python

  Second, it means that no special syntax is necessary if you want to explicitly reference or call the method from a particular class. In C++, if you want to use a method from a base class which is overridden in a derived class, you have to use the :: operator – in Python you can write baseclass.methodname(self, <argument list>). This is particularly useful for __init__() methods, and in general in cases where a derived class method wants to extend the base class method of the same name and thus has to call the base class method somehow.ide

  Finally, for instance variables it solves a syntactic problem with assignment: since local variables in Python are (by definition!) those variables to which a value is assigned in a function body (and that aren’t explicitly declared global), there has to be some way to tell the interpreter that an assignment was meant to assign to an instance variable instead of to a local variable, and it should preferably be syntactic (for efficiency reasons). C++ does this through declarations, but Python doesn’t have declarations and it would be a pity having to introduce them just for this purpose. Using the explicit self.var solves this nicely. Similarly, for using instance variables, having to write self.var means that references to unqualified names inside a method don’t have to search the instance’s directories. To put it another way, local variables and instance variables live in two different namespaces, and you need to tell Python which namespace to use.函數

 

中文翻譯(原創):this

  這種作法是借鑑自Modula-3, 在不少方面來看到都是頗有用的。
  首先,讓你很明顯看出這是在使用一個實例方法或屬性,而不是一個局部變量編碼

      即使對類的定義不清楚,遇到self.x或者self.meth()都會明白這是實例屬性或方法。idea

在C++中,若是局部變量未聲明,編譯器會告訴你;可是,在python中,因爲局部變量不須要聲明,
使得你須要在類的定義中去找。一些C++及Java的編碼規範須要使用m_作前綴來定義實例屬性,在這些語言中,這種明確的
定義是很是有用的。
spa

  其次,這樣作能夠不須要特別的語法,來供一個class去調用這些方法翻譯

     在C++中,若是你想在子類中調用基類中被那些被子類複寫的方法,htm

你須要使用::操做符 - 在Python中,你可使用baseclass.methodname(self, <argument list>)的
方式來調用,這對於__init__()方法特別有用,另外對於子類中複寫的方法想要調用基類中同名方法時也很是有效。

  最後,屬性解決了賦值的語法問題

     由於python中的局部變量都是在函數體中被賦值的(它們沒有被定義成全局變量),
須要有一種方式來告訴解釋器某一個賦值操做是賦給屬性仍是局部變量,這樣能夠更好地語法檢查(爲了高效)。C++經過聲明
來作到這一點,可是Python沒有聲明,爲此而引入聲明也不划算。明確地使用self.var能夠很好地解決這個問題。
使用self.var意味着:在一個方法中引用一個非法的變量不須要在實例目錄中去查找(這一部分不太可以理解)。另外,局部
變量和實例變量存在於不一樣的命名空間,你須要告訴python到底應該使用哪一個命名空間。

 

參考: http://docs.python.org/2/faq/design.html#why-must-self-be-used-explicitly-in-method-definitions-and-calls

相關文章
相關標籤/搜索