a python pitfall
Mark Lawrence
breamoreboy at yahoo.co.uk
Thu May 14 16:12:35 EDT 2015
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
More information about the Python-list
mailing list