Dictionary self lookup

Norberto Lopes shelika.void at gmail.com
Wed Jun 24 20:07:00 CEST 2009


On Jun 24, 6:26 pm, Scott David Daniels <Scott.Dani... at Acm.Org> wrote:
> Norberto Lopes wrote:
> > On Jun 24, 1:21 pm, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> >> Norberto Lopes wrote: ...
> >>> config = {"home" : "/home/test"}
> >>> config["user1"] = config["home"] + "/user1"
> >>> config["user2"] = config["home"] + "/user2"
> >>> config["python-dev"] = config["user1"] + "/py-dev"
>
> I'd write this as:
>     home = "/home/test"
>     config = {"home" : home, "user1" : home + "/user1",
>               "user2" : home + "/user2", "python-dev" : home + "/py-dev"}
>
> or even (if the list gets much longer):
>
>     home = "/home/test"
>     config = {"home" : "", "user1" : "/user1", "user2" : "/user2",
>               "python-dev" : "/py-dev"}
>     for key, entry in config.iteritems():
>         config[key] = home + entry
>
> --Scott David Daniels
> Scott.Dani... at Acm.Org

Yeah, there are a lot of options. That was just an example. Btw, I had
"python-dev" depending on "user1" and "user1" depending on "home"
precisely so that the way you suggested would get complicated. Yours
examples fail in the python-dev key-value. You'd need an "extra"
step ;)




More information about the Python-list mailing list