Looking for magic method to override to prevent dict(d) from grabbing subclass inst d contents directly

Mike Meyer mwm at mired.org
Tue Nov 22 09:30:49 EST 2005


bokr at oz.net (Bengt Richter) writes:
> Has anyone found a way besides not deriving from dict?
> Shouldn't there be a way?
> TIA
> (need this for what I hope is an improvement on the Larosa/Foord OrderedDict ;-)
>
> I guess I can just document that you have to spell it dict(d.items()), but I'd
> like to hide the internal shenanigans ;-)

So why not just hide them:

>>> class md(dict):
...  def __new__(cls, arg):
...   return dict.__new__(cls, arg.items())


I'm not sure exactly what you mean by "grabbing sublass inst d contens
directly", but if dict(d.items()) does it, the above class should do
it as well. Of course, *other* ways of initializing a dict won't work
for this class.

    <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list