In Python 3, I should be able to use super()
in a class method as a substitute for the parent class:
class demodict(OrderedDict):
def __setitem__(self, key, val):
print(key)
super().__setitem__(self, key, val)
The above behaves as expected when I instantiate a demodict()
and add values to it. But if I use it as the data type for a ConfigParser
object, something goes wrong:
>>> Config = configparser.ConfigParser(dict_type=demodict)
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
conf = configparser.ConfigParser(dict_type=demodict)
File ".../anaconda/lib/python3.4/configparser.py", line 588, in __init__
self._proxies[default_section] = SectionProxy(self, default_section)
File "<pyshell#14>", line 5, in __setitem__
super().__setitem__(self, key, val)
File ".../anaconda/lib/python3.4/collections/__init__.py", line 67, in __setitem__
if key not in self:
TypeError: unhashable type: 'demodict'
If I replace OrderedDict
with plain dict
as the parent class, the error gets even weirder:
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
conf = configparser.ConfigParser(dict_type=demodict)
File ".../anaconda/lib/python3.4/configparser.py", line 588, in __init__
self._proxies[default_section] = SectionProxy(self, default_section)
File "<pyshell#7>", line 6, in __setitem__
super().__setitem__(self, key, val)
TypeError: expected 2 arguments, got 3
If instead of super()
I write OrderedDict
or dict
explicitly, I can use demodict
as the dict_type
with no problems. Can someone explain what is going on? (Since there is an easy work-around, I'm more curious about the cause than about the solution...)