Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90622

a python pitfall

Date 2015-05-14 13:06 -0500
Subject a python pitfall
From Billy Earney <billy.earney@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.11.1431626772.17265.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

a python pitfall Billy Earney <billy.earney@gmail.com> - 2015-05-14 13:06 -0500
  Re: a python pitfall Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-15 12:03 +1000

csiph-web