如何动态覆盖__setitem__? (无子类) [英] How to dynamically override __setitem__? (no subclass)
问题描述
在 Python2.7 中,我无法覆盖某些内置函数,例如'__setitem __'(尽管在我测试的先前版本中也是如此)
I'm not able to override some builtin functions, such as '__setitem__', in Python2.7 (although the same occurs in previous versions I tested)
尽管我知道通过子类很容易做到这一点,但这不是我想要的,但我需要能够动态地覆盖这些方法.
Although I know this is easy to do via subclassing, this is not what I want here, I need to be able to dynamically override these methods.
很明显,当我的类是' object '的子类时,被覆盖的方法总是最终调用原始类,但是当我的类不是' object ',它的工作原理是:
Apparently, when my class is a subclass of 'object', the overridden method always ends up calling the original one, but when my class is not an 'object', it works:
>>> def new_implementation(k, v):
... print 'New implementation'
...
### class that extends object
>>> class ExtendsObject(object):
... def __setitem__(self, k, v):
... print 'ExtendsObject implementation'
...
### Checking implementation
>>> obj = ExtendsObject()
>>> obj[0]=0
ExtendsObject implementation
### trying to override setitem, no success
>>> obj.__setitem__ = new_implementation
>>> obj[0]=0
ExtendsObject implementation
### class that does NOT extends object
>>> class DoesNotExtend:
... def __setitem__(self, k, v):
... print 'DoesNotExtend implementation'
...
### Checking implementation
>>> obj_2 = DoesNotExtend()
>>> obj_2[0]=0
DoesNotExtend implementation
### overriding now works!
>>> obj_2.__setitem__ = new_implementation
>>> obj_2[0]=0
New implementation
出于某种原因,似乎这些内置函数的对象使用了不同的方法解析顺序.
For some reason, it seems that objects use some different method resolution order for these built-in functions.
这是一个错误吗?我在做错什么吗?
Is this a bug? Am I doing something wrong?
推荐答案
对于旧类,每次需要在实例上查找特殊方法.新型类仅在实例的 type 上查找特殊的方法,而不是在实例的字典本身中查找-这就是为什么您看到所看到的行为的原因.
For old-style classes, special methods were looked up on the instance each time they were needed. New-style classes only look up special methods on the type of the instance, not in the instance's dictionary itself -- that's why you see the behaviour you see.
(如果您不知道,则新样式类是直接或间接从object
派生的类.)
(In case you don't know -- a new-style class is a class directly or indirectly derived from object
.)
这篇关于如何动态覆盖__setitem__? (无子类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!