[Tutor] How to load a dict into a dict subclass?
Albert-Jan Roskam
fomcl at yahoo.com
Tue Oct 27 20:02:13 CET 2009
I thought reimplementing dict was a matter of defining a __dict__ attribute in the Bar class? And isn't the normal dict inherited anyway? (through __super__)
Or, if one would want to disable inheritance of the normal dict, one could use __slots__. Right?
Or did I misinterpret the New Testament of my Python Bible?
Cheers!!
Albert-Jan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Before you criticize someone, walk a mile in their shoes, that way
when you do criticize them, you're a mile away and you have their shoes!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--- On Tue, 10/27/09, Alan Gauld <alan.gauld at btinternet.com> wrote:
> From: Alan Gauld <alan.gauld at btinternet.com>
> Subject: Re: [Tutor] How to load a dict into a dict subclass?
> To: tutor at python.org
> Date: Tuesday, October 27, 2009, 6:45 PM
>
> "John" <jfabiani at yolo.com>
> wrote
>
> >> > >>> class Bar(dict):
> >> > >>> pass
> >> > >>>
> >> > >>> foo = {'a':100, 'b':200, 'c':
> 300}
> >> > >>> myvar = Bar()
> >> > >>> myvar.update(foo)
> >> > >>> myvar
> >> >
> >> > {'a': 100, 'c': 300, 'b': 200}
> >
> > Hey guru's could one of you explain why such a
> subclass is needed. How would
> > it be used. I'm not sure but does not the
> deepcopy() work as above?
>
> This particular subclass does nothing different to the
> normal dict so its
> never needed. But it's only a demo of how to use the
> standard dict
> methods in a subclass.
>
> In general you might subclass a dict if you wanted to
> change the
> behaviour of some specific method, or maybe add some new
> methods.
> Or maybe you wanted a dictionary that could take a list as
> a key...
> or even take a list of keys and return a list of values.
>
> Lots of reasons for wanting to modify the standard dict
> behaviour.
>
> -- Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list