【发布时间】:2013-10-24 19:18:05
【问题描述】:
我面临无法继承超类属性值的问题。我已经调用了超类构造函数,现在尝试检查继承的值。
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__。关于这个问题有很多以前的问题。 -
@shx2:这个问题的答案是 Python 2 特有的。
标签: python oop inheritance