Dictionary self lookup

Diez B. Roggisch deets at nospam.web.de
Wed Jun 24 06:06:37 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 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.

I doubt this will ever be accepted, but out of curiosity: *why* do you want

And if you are willing to put some constraints on you keys, it would be easy
enough to do this:

def wicked_dict(**kwargs):
    mappings = [(name, source) for name, source in kwargs.iteritems() if
    res = dict((key, value) for key, value in kwarges.iteritems() if not
    for name, source in mappings:
        res[name] = res[source]
    return res


More information about the Python-list mailing list