getattr
在Python 高级特性之定制类中已经提到过 __getattr__
及其的一种应用 (Restful API), 除此之外, __getattr__
还可以用来实现组合模式 (需要配合反射) -- 组合优于继承.
如果 Composite
需要修改 Element
的行为, 那么定义一个同名的属性就行了, 其他的想直接"继承"的属性,通通交给 __getattr__
就行了.
Last updated
在Python 高级特性之定制类中已经提到过 __getattr__
及其的一种应用 (Restful API), 除此之外, __getattr__
还可以用来实现组合模式 (需要配合反射) -- 组合优于继承.
如果 Composite
需要修改 Element
的行为, 那么定义一个同名的属性就行了, 其他的想直接"继承"的属性,通通交给 __getattr__
就行了.
Last updated