class x():
def __init__(self):
self.z=2
class hi():
def __init__(self):
self.child=x()
f=hi()
print f.z
我希望它打印出来2。
基本上我想将对该班级的任何呼叫转发给另一个班级。
class x():
def __init__(self):
self.z=2
class hi():
def __init__(self):
self.child=x()
f=hi()
print f.z
我希望它打印出来2。
基本上我想将对该班级的任何呼叫转发给另一个班级。
最简单的方法是实施__getattr__:
class hi():
def __init__(self):
self.child=x()
def __getattr__(self, attr):
return getattr(self.child, attr)
这有一些缺点,但它可能适用于您有限的用例。您可能还想实施__hasattr__and __setattr__。
Python 语法是:
class hi(x):
要说hi继承(应该是子级)x。
.
注意:为了hi拥有属性z(因为这是在hi's中__init__)x.__init__需要显式运行在x. 那是,
class hi(x):
def __init__(self):
x.__init__(self)