Dictionary self lookup

Carl Banks pavlovevidence at gmail.com
Thu Jun 25 04:42:03 CEST 2009

On Jun 24, 2:39 am, Norberto Lopes <shelika.v... at gmail.com> 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.

If you don't mind abusing Python syntax, you can do it using something
like this:

def DictMaker(name,bases,dct):
    return dct

class a:
    __metaclass__ = DictMaker

    home = "/home/test"
    user1 = home + "/user1"
    user2 = home + "/user2"
    python_dev = user1 + "/py-dev"

print a

Carl Banks

More information about the Python-list mailing list