By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,526 Members | 1,618 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,526 IT Pros & Developers. It's quick & easy.

Re: Default Argument Question

P: n/a
On Wed, Oct 29, 2008 at 9:14 AM, Paulo J. Matos <po**@ecs.soton.ac.ukwrote:
Hi all,

Going through the tutorial brought up a question. Consider the functions:

def f(a, L=[]):
L.append(a)
return L

print f(3)
print f(9)
print f(7)

def f1(i = 0):
i = i + 1
print i

f1()
f1()
f1()
f1()

Since the f accumulates the values in L, I was expecting to see i
printing 1,2,3,4 but this doesn't happen.
Can someone please explain why and what is going on beneath the veil?
http://effbot.org/zone/default-values.htm explains this FAQ quite
well. Note that the "accumulation" behavior of lists is considered an
aberration and is usually not desired.

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
>
Cheers,

--
Paulo Jorge Matos - pocmatos @ gmail.com
--
http://mail.python.org/mailman/listinfo/python-list
Oct 29 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Chris Rebert a écrit :
(snip)
Note that the "accumulation" behavior of lists is considered an
aberration
By who ?

Oct 29 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.