[Tutor] Surprising behaviour of optional argument

Danny Yoo dyoo at hashcollision.org
Thu Jul 17 19:34:09 CEST 2014


On Jul 17, 2014 10:22 AM, "Jose Amoreira" <ljmamoreira at gmail.com> wrote:
>
> Hello
> I stumbled on something I found surprising. I'm using standard python
(cpython, I guess) 2.7 on openSuse 13.1.
>
> Consider the function
>
> In [2]: def f(x,y=[]):
>    ...:     print y
>    ...:     y.append(x)
>    ...:     return x
>

Yeah; the default value is not reevaluated between calls.  It's a common
gotcha.  Here are a few links to read more:

http://effbot.org/zone/default-values.htm

http://docs.python-guide.org/en/latest/writing/gotchas/#mutable-default-arguments

Good luck!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140717/1c422c07/attachment.html>


More information about the Tutor mailing list