[英]python: selectively override __getattr__
I have successfully overridden the __getattr__
method in order to allow for complex behavior. 我已经成功重写了__getattr__
方法,以允许复杂的行为。 However I still would like to allow some of the __builtin__
functions to be default. 但是,我仍然希望允许某些__builtin__
函数为默认函数。 For example, I have used __getattr__
to handle __add__
, __sub__
, __mul__
, etc. However, the __iadd__
, __isub__
, __imul__
, etc. are trying to use the __getattr__
method and are throwing an error. 例如,我已经使用__getattr__
处理__add__
, __sub__
, __mul__
,等等。然而, __iadd__
, __isub__
, __imul__
等正在尝试使用__getattr__
方法,并抛出一个错误。 I could just define their behaviors as well, but I think that allowing these methods to run as default would be better. 我也可以定义它们的行为,但是我认为允许这些方法默认运行会更好。
Bottom line: I would like to allow __getattr__
to filter which attributes it handles, and which it allows to run as default. 底线:我想允许__getattr__
过滤它处理的属性,以及允许它默认运行的属性。
Answered my own question. 回答了我自己的问题。 I used if statements to handle groups of attributes, and then as a last resort, used return getattr(my_cls_name, attr)
to allow any other builtin function to run normally. 我使用if语句来处理属性组,然后作为最后的选择,使用return getattr(my_cls_name, attr)
来允许任何其他内置函数正常运行。 So now when it realizes there is no __iadd__
handler, it uses my __add__
instead. 因此,现在当它意识到没有__iadd__
处理程序时,它将使用我的__add__
代替。 same with the other operators. 与其他运营商相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.