a python pitfall

Billy Earney billy.earney at gmail.com
Thu May 14 14:06:09 EDT 2015


Hello friends:

I saw the following example at
http://nafiulis.me/potential-pythonic-pitfalls.html#using-mutable-default-arguments
and
did not believe the output produced and had to try it for myself....

def foo(a,b,c=[]):
    c.append(a)
    c.append(b)
    print(c)

foo(1,1)
foo(1,1)
foo(1,1)

produces:
[1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1, 1, 1]

One would expect the following output:
[1, 1]
[1, 1]
[1, 1]

Doesn't this valid the zen of python: "Explicit is better than implicit."  ?

Thanks!
Billy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150514/4fad8403/attachment.html>


More information about the Python-list mailing list