[issue24685] collections.OrderedDict collaborative subclassing
Eric Frederich
report at bugs.python.org
Thu Jul 23 19:31:28 CEST 2015
Eric Frederich added the comment:
Raymond,
Thanks for the explanation of your reasoning.
Could you please provide an example of how to create a cooperative subclass of OrderedDict?
I have attempted to make one.
I succeeded to make it work where the previous example failed but in doing made the example that used to work now fail.
I have attached my attempt at making a cooperative OrderedDict in inj2.py
class coop_OrderedDict(OrderedDict):
"""
A cooperative version of OrderedDict
"""
def __setitem__(self, k, v, **kwargs):
# OrderedDict calls dict.__setitem__ directly skipping over LoggingDict
# fortunately we can control this with dict_setitem keyword argument
# calculate OrderedDict's real parent instead of skipping right to dict.__setitem__
# though depending on the hierarchy it may actually be dict.__setitem__
m = super(OrderedDict, self).__setitem__
# dict_setitem wants an unbound method
unbound_m = m.im_func
return super(coop_OrderedDict, self).__setitem__(k, v, dict_setitem=unbound_m)
In Python2 it fails with:
Traceback (most recent call last):
File "/tmp/inj2.py", line 51, in <module>
old1['hooray'] = 'it worked'
File "/tmp/inj2.py", line 30, in __setitem__
return super(LoggingDict, self).__setitem__(k, v)
File "/tmp/inj2.py", line 18, in __setitem__
unbound_m = m.im_func
AttributeError: 'method-wrapper' object has no attribute 'im_func'
In Python3 both cases fail.
----------
Added file: http://bugs.python.org/file39998/inj2.py
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24685>
_______________________________________
More information about the Python-bugs-list
mailing list