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


Groups > comp.lang.python > #90631

Re: a python pitfall

From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: a python pitfall
Date 2015-05-14 21:12 +0100
References <CAB1ii-cFpbmdjQfRA_J3TBUZ+Gx8e8vLvqTLo2R-f0ygkykV1A@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.18.1431634364.17265.python-list@python.org> (permalink)

Show all headers | View raw


On 14/05/2015 19:06, Billy Earney wrote:
> Hello friends:
>
> I saw the following example at
> http://nafiulis.me/potential-pythonic-pitfalls.html#using-mutable-default-arguments

Thanks for this link, the title "Engineering Fantasy" could have been 
made for the troll who's just arrived back here after a couple of years 
being absent.

> 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]
>

One wouldn't expect the above because one knows better :)  This comes up 
every few months, has probably come up every few months since Python was 
first let loose on the world, will probably keep coming up every few 
months, and will cease coming up when Python is no longer used as 
something better has taken its place or the planet no longer exists, 
whichever comes first.  The most recent I recall was just six days ago 
http://comments.gmane.org/gmane.comp.python.general/776290

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

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Thread

Re: a python pitfall Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-05-14 21:12 +0100

csiph-web