Dictionary self lookup

Diez B. Roggisch deets at nospam.web.de
Wed Jun 24 07:21:30 EDT 2009


Norberto Lopes wrote:

> On Jun 24, 11:59 am, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
>> Norberto Lopes wrote:
>> > Hi all.
>> > Assuming that python dictionaries already provide a bit of "shoot
>> > yourself in the foot", I think what I have in mind would not be so
>> > bad.
>>
>> What kind of foot-shooting do you have in mind?
>>
> 
> a = { "foo" : { "bar" : "moo" }}
> a["bar"] = a["foo"]
> print a
> {'foo': {'bar': 'moo'}, 'bar': {'bar': 'moo'}}
> a["foo"]["bar"] = a["foo"]
> print a
> {'foo': {'bar': {...}}, 'bar': {'bar': {...}}}
> 
> (I know it's not a C shoot in the foot or something but still...)

And something working alike for lists and objects. Cyclic references are a
fact of (programming)-life unless you go fully functional - and then you'd
certainly miss it sometimes..

> config = {"home" : "/home/test"}
> config["user1"] = config["home"] + "/user1"
> config["user2"] = config["home"] + "/user2"
> config["python-dev"] = config["user1"] + "/py-dev"
> 
> 
> Now, if you change config["home"] you'd have to redo all of the other
> entries. With the first one (assuming pointers to the entries)
> everything would get updated. Although python does not handles dict
> keys/values this way. Now that I think of this there would be a lot
> more than just syntactic sugar for this.

Things that are re-done usually are useful being put into a function. Then
redoing becomes easier :)

Diez



More information about the Python-list mailing list