Maintaining a list

Scott David Daniels Scott.Daniels at Acm.Org
Fri Feb 20 23:13:39 EST 2004


Steve wrote:

> Thomas Philips wrote:
>> I'm teaching myself programming using Python and want to build a list
>> inside a function (rather like using a static variable in a Fortran
>> subroutime - the list must not disappear as soon as the subroutine is
>> exited). What's the simplest way to do this?
>...
> mylist = []
> 
> def myroutine(x)
>     global mylist
>     mylist.append(x)
>     print mylist
>     return None

For the shortest (not simplest conceptually, but good to learn why it
works) solution, I'd propose:

     mylist = []
     myroutine = mylist.append


-- 
-Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list