in-place value modification (was Re: lambda without vars?)

Jeff Pinyan jeffp at crusoe.net
Wed Mar 15 14:35:08 EST 2000


On Mar 15, Jeff Pinyan said:

>>>> a = [1,2,3]
>>>> def app(name,val):
>...   tmp = globals()[name]
>...   tmp.append(val)
>...
>>>> app('a',4)
>>>> a
>[1, 2, 3, 4]

This could also be done just by

>>> a = [1,2,3]
>>> def foo(this):
...  this.append(1)
...
>>> foo(a)
>>> a
[1, 2, 3, 1]

but I brought in globals() to preface the ugly chomp() example.

-- 
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve             japhy at pobox.com
http://www.pobox.com/~japhy/                  http://pinyaj.stu.rpi.edu/
PerlMonth - An Online Perl Magazine            http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc.    http://www.perlarchive.com/




More information about the Python-list mailing list