"specialdict" module

Georg Brandl g.brandl at gmx.net
Mon Apr 4 12:53:47 EDT 2005


Georg Brandl wrote:
> Michele Simionato wrote:
>> About not using super: you might have problems in multiple inheritance.
>> Suppose I want to use both your defaultdict and a thirdpartdict. A
>> subclass
>> 
>> class mydict(defaultdict, thirdpartdict):
>>    pass
>> 
>> would not work if thirdpartdict requires a non-trivial __init__ , since
>> without super in defaultdict.__init__ you would just call dict.__init__
>> and not thirdpartdict.
> 
> Right. I thought about a combined defaultdict/keytransformdict,
> which seems to be easy to create with the current implementation:
> 
> class defaultkeytransformdict(defaultdict, keytransformdict):
>     pass
> 
> At least I hope so. This is another argument against the initializing
> of defaultfactory or keytransformer in __init__.
> 
> mfg
> Georg
> 
> Here comes the current module (keytransformdict should be working now
> for all dict methods):
> 
> # specialdict - subclasses of dict for common tasks
> #
> 
> class NoDefaultGiven(Exception):
>     pass
> 
> class defaultdict(dict):
>     __slots__ = ['_default']
> 
>     def __init__(self, *args, **kwargs):
>         self._defaulttype = 0
          ^^^^^^^^^^^^^^^^^^^^^

This must read "self.cleardefault()", of course.

mfg
Georg



More information about the Python-list mailing list