Dictionary self lookup

Carl Banks pavlovevidence at gmail.com
Sun Jun 28 05:39:47 EDT 2009

On Jun 27, 4:33 am, Jure Erznožnik <jure.erznoz... at gmail.com> wrote:
> When you write code like
> config = {"home" : "/home/test"}
> config["user1"] = config["home"] + "/user1"
> config["user1"] isn't stored in memory as config["home"] + "/user1",
> but as a concatenated string ("/home/test/user1"), composed of both
> those strings. The reference to original composing strings is lost at
> the moment the expression itself is evaluated to be inserted into the
> dict.
> There's no compiler / interpreter that would do this any other way. At
> least not that I know of.

It's called Lazy Evaluation and there are a bunch of tools that do it
implicitly and automatically.  Off hand I can think of make and
Mathematica.  I know there are some functional programming languages
that do it as well.

Also there are languages like Lisp that have strong syntactic support
for lazy evaluation although it's not done implicitly.

Not Python, though.

> So best suggestion would be to simply do an object that would parse
> strings before returning them. In the string itself, you can have
> special blocks that tell your parser that they are references to other
> objects.

[snip example]

> But you sure better not expect this to be included in language syntax.
> It's a pretty special case.

That is a very good suggestion that might be a better solution to the
OP's problem.

You are right that is would require a bit of work.  Python does have
string interpolation methods that can do keyword-based substitution,
but they don't work recursively, and it's not straightforward to get
it to work recursively.

Carl Banks

More information about the Python-list mailing list