将类的属性委托给另一个属性的属性
delegate-propert的Python项目详细描述
代表
用于委派的python库(元编程特性)
此库添加可用于委托的@delegate
装饰器
来自现有类属性的属性。例如:
fromdelegateimportdelegateclassParent:def__init__(self):self.a="a"self.b="b"self.d="d"# The delegate decorator makes .a and .b available on Child, through its# "parent" attribute, as though Child had an a and b attribute itself.@delegate("a","b",to="parent")classChild:def__init__(self):self.parent=Parent()self.c="c"instance=Child()assertinstance.a=="a"raised=Falsetry:# But d is not availableinstance.dexcepte:raised=Trueassertraised