Dictionary self lookup

Diez B. Roggisch deets at nospam.web.de
Wed Jun 24 05:59:13 EDT 2009


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?
> 
> What do you think of dictionaries having a self lookup in their
> declaration?
> 
> Be able to do this:
> 
> a = {"foo" : "foo1", "bar" : a["foo"]} # or with another syntax
> 
> instead of:
> 
> a = { "foo" : "foo1" }
> a["bar"] = a["foo"]
> 
> Maybe I'm murdering python syntax/philosophy right here so let me know
> if that's the case.
> I was thinking this could probably be done in python abstract tree but
> as I never looked into it I may be wrong. I'm willing to make the
> effort, provided I get some directions and that this idea is worth it.
> 
> Any feedback is welcome.

Obviously the proposed syntax can't work, as at the time of the dictionary
construction the name the dict is bound to is either not known, or even
bound to *another* dict. 

Additionally, the code would be by no means more efficient than the
above "long" version, as whatever notation you chose, it won't help to deal
with the fact that the dict-object itself, and also a potentially reference
key, aren't already available.

So behind the curtain, the exact same logic would apply, with all
runtime-costs.

Which leaves us with the question: why the heck do you want this? 

Diez



More information about the Python-list mailing list