Scope troubles with a swap function

Markus Schaber markus at schabi.de
Sun Aug 12 10:06:16 EDT 2001


Hi,

Steve Holden <sholden at holdenweb.com> schrub:

> "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 that Python cannot handle scope. Sure you have namespaces,
>> but that doesn't help in this situation. In Tcl I would just say..
>>
>> proc swap {a b} {
>>    upvar a x
>>    upvar b y
>>    set z x
>>    set x y
>>    set y z
>> }
>> swap a b
>>
> Not knowing Tcl, I'm assuming that "upvar" effectively means "get
> address of" (a bit like the C "&" operator)?

No - it means "bind the local variable x to the caller's local variable 
whose name is in a". When you change x, then the callers variable is 
also changed, means, you conceptually poke in foreign stack frames :-(


markus
-- 
1) Customers cause problems.
2) Marketing is trying to create more customers.
Therefore:
3) Marketing is evil.  (Grand Edwards in comp.lang.python)



More information about the Python-list mailing list