Scope troubles with a swap function

Alex Martelli aleaxit at yahoo.com
Fri Aug 10 16:10:42 EDT 2001


"Cris A. Fugate" <fugate at lucent.com> wrote in message
news:Pine.LNX.4.33.0108101302250.611-100000 at IL0015ACSULLIV3.ih.lucent.com...
> Hi, I am trying to port some Tcl stuff to Python. Its kind of disturbing
    ...
> BTW, I also noticed that python is rather weak in variable
> substitution. In Tcl I can say..
>
> set x 5; set y "x"
> expr $$y + 1
> =>6
>
> I cant do this in python without resorting to something like
> dictionaries..
>
> y={'x':'z', 'z':5}
> y.get(y.get('x')) + 1
> =>6

You can do hardly anything in Python without resorting to
dictionaries "under the covers" (dictionaries is where most
types keep their attributes -- modules, classes, etc), but
why is that a problem?  The exact equivalent of your Tcl
"$$y" in Python is "eval(y)" -- it's a HORRID idea to USE
it, of course, but, if you crave for it, it IS right there.

Your dictionary-based "solution" doesn't look very useful
to me, either.  What is this "extra level of indirectness"
actually BUYING you -- i.e. WHY are you using it, except
because of habits stemming from Tcl practice?  Anyway,
the normal Python syntax for the last expression you
write would be y[y['x']]+1.


Alex






More information about the Python-list mailing list