NEWBIE: lists as function arguments
news.chartermi.net
jponiato at chartermi.nojunk
Fri Nov 7 19:00:31 EST 2003
Thanks, everyone - I knew you folks could help me understand. This is still
me, JP, posting from another account.
"Joe Poniatowski" <jponiato at ford.nospambot.com> wrote in message
news:bogkj1$cfk11 at eccws12.dearborn.ford.com...
> From what I've read in the tutorial and FAQ, I should be able to pass a
> mutable object like a list as a function argument, and have the caller see
> changes made to the list by the function. I must be doing something
wrong -
> the function in this case assigns items to the list, but in the main
module
> it's still empty:
>
> Python 2.3.2 (#1, Oct 23 2003, 11:10:37)
> [GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-81)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> ls = []
> >>> def f1(l2=[]):
> ... l2 = ['word1','word2']
> ... print l2
> ...
> >>> f1(ls)
> ['word1', 'word2']
> >>> ls
> []
> >>>
>
> However, I can return the list and it works OK:
> >>> ls = []
> >>> def f1(l2=[]):
> ... l2 = ['word1','word2']
> ... print l2
> ... return l2
> ...
> >>> ls = f1(ls)
> ['word1', 'word2']
> >>> ls
> ['word1', 'word2']
>
> Am I missing something or just interpreting the documentation wrong?
> Jp
>
>
More information about the Python-list
mailing list