我面臨着超類屬性值繼承的不能。我已經調用超類的構造函數,現在試圖檢查出繼承的值。Python:類數據的繼承(如果超類對象已經被初始化)
class base:
def __init__(self, x):
self.x = x
print(self.x)
class derive(base):
def __init__(self):
print(self.x + 1)
print("base class: ")
b = base(1) <-- Creating superclass instance
print("derive class: ")
d = derived() <-- Inheriting. Failure.
爲什麼我不能這樣做?爲了得到x屬性,我是否應該明確地將底層對象傳遞給繼承對象?
您需要從派生類中調用基類「__init__」。關於這個問題有很多以前的問題。 – BrenBarn
可能的重複[如何在Python中使用'超'?](http://stackoverflow.com/questions/222877/how-to-use-super-in-python) – shx2
@ shx2:這個問題是Python 2 - 具體的答案。 –