NEWBIE: lists as function arguments

  • Thread starter Joe Poniatowski
  • Start date
J

Joe Poniatowski

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.
.... l2 = ['word1','word2']
.... print l2
....
f1(ls) ['word1', 'word2']
ls []

However, I can return the list and it works OK:
.... 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
 
P

Peter Otten

Joe said:
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']

You are rebinding the local variable l2 to another list here.
To change the list given as the argument, do l2.append("word3")
or l2[:] = ["word1", "word2"], e. g.:
.... alist[:] = ['word1', 'word2']
....
l1 = ['so', 'what']
f1(l1)
print l1 ['word1', 'word2']

By the way, it is usually a bad idea to use mutable objects as default
arguments:
.... alist.extend(words.split())
.... return alist
....
.... print add(w)
....
['x', 'y']
['x', 'y', 'a', 'b']
I doubt this is what you expected. To default to an empty list, use

def add(words, alist=None):
if alist is None: alist = []
# ...

instead.

Peter
 
M

Michael Geary

Joe Poniatowski:
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
[]

You may be thinking of a language that has variables and assignment
statements that change those variables. Python doesn't actually have either
of these. Instead, it has names that are bound to objects.

Your code "l2 = ['word1','word2']" *rebinds* the name "l2" to a new list
object. It doesn't affect the object that l2 was bound to before this
statement.
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']

This works because when you say "ls = f1(ls)" you're rebinding ls to the
object that f1() returns. You still haven't changed the list that ls was
originally bound to--ls now refers to the new object.

-Mike
 
N

news.chartermi.net

Thanks, everyone - I knew you folks could help me understand. This is still
me, JP, posting from another account.

Joe Poniatowski said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top