可能重複:
Why do I have to specify my own class when using super(), and is there a way to get around it?爲什麼它是強制性的給類名,而使用超()在Python
我在讀這本書 - "Core Python Programming"
,我真的覺得很漂亮我會說。但是當我在研究主題繼承時,我有點困惑。
這本書某處說,我們可以使用super()
來調用super class method
,它會爲我們找到基類方法,而且我們也不需要通過self
明確的,像我們這樣做不超..
這裏的示例代碼: -
# Invoking super class method without super() .. Need to pass `self` as argument
class Child(Parent):
def foo(self):
Parent.foo(self)
# Invoking with super().
# No need to pass `self` as argument to foo()
class Child(Parent1, Parent2):
def foo(self):
super(Child, self).foo()
print 'Hi, I am Child-foo()'
我的問題是 - 爲什麼我們必須通過classname
到super()
通話。作爲類名可以推斷來自super調用的類d ..
因此,由於super()是從 class Child
中調用的,因此classname應該是隱式的。那麼爲什麼我們需要這個?
編輯: - 給Child
作爲super()的參數沒有意義,因爲它沒有給出任何信息。我們可以使用super(self)
..並且該方法已經在超級它們按照括號內的順序排列。
在Python 3中,你可以使用'super()'。 [Python的super()認爲超級!](http://rhettinger.wordpress.com/2011/05/26/super-considered-super/) – jfs
OOPs。無法訪問該鏈接在辦公室.. :(將看到它回家.. –