Inheriting dictionary

Pavel Panchekha pavpanchekha at gmail.com
Tue Aug 18 17:19:05 EDT 2009


On Aug 18, 5:11 pm, "Jan Kaliszewski" <z... at chopin.edu.pl> wrote:
> 18-08-2009 o 22:27:41 Nat Williams <nat.willi... at gmail.com> wrote:
>
>
>
> > On Tue, Aug 18, 2009 at 2:44 PM, Pavel Panchekha  
> > <pavpanche... at gmail.com>wrote:
>
> >> I want a dictionary that will transparently "inherit" from a parent
> >> dictionary. So, for example:
>
> >> """
> >> a = InheritDict({1: "one", 2: "two", 4: "four"})
> >> b = InheritDict({3: "three", 4: "foobar"}, inherit_from=a)
>
> >> a[1] # "one"
> >> a[4] # "four"
> >> b[1] # "one"
> >> b[3] # "three"
> >> b[4] # "foobar"
> >> """
>
> >> I've written something like this in Python already, but I'm wondering
> >> if something like this already exists, preferably written in C, for
> >> speed.
>
> > Why complicate this with a custom object?  Just use regular dicts and  
> > make b a copy of a.
>
> > a = {1: 'one', 2: 'two', 4: 'four'}
> > b = dict(a)
> > b[3] = 'three'
> > b[4] = 'foobar'
>
> Because, as I understand Pavel's intent, it has to work dynamically
> (e.g. changes in 'a' reflect in behaviour of 'b'), and obviously not
> only for such trivial examples like above.
>
> *j
>
> --
> Jan Kaliszewski (zuo) <z... at chopin.edu.pl>

That is indeed the point.



More information about the Python-list mailing list