Default Value

Rick Johnson rantingrickjohnson at gmail.com
Wed Jun 19 15:38:39 EDT 2013


On Wednesday, June 19, 2013 2:17:35 PM UTC-5, Ahmed Abdulshafy wrote:
> I'm reading the Python.org tutorial right now, and I found
> this part rather strange and incomprehensible to me>
> 
> Important warning: The default value is evaluated only
> once. This makes a difference when the default is a
> mutable object such as a list, dictionary, or instances of
> most classes
> 
> def f(a, L=[]):
>     L.append(a)
>     return L
> 
> print(f(1))
> print(f(2))
> print(f(3))
> 
> This will print
> 
> [1]
> [1, 2]
> [1, 2, 3]
> 
> How the list is retained between successive calls? And
> why?

By the evil hands of an illogical consistency.

Have you ever heard the old adage: "The road to hell is
paved in good intentions"? Well, apparently the original
designers of the language called in sick the day that class
was taught. It's unfortunate because the same functionality
that this "intention" claims to solve can be reproduced
easily, and in a less astonishing manner, by the programmer
himself.

  http://en.wikipedia.org/wiki/Principle_of_least_astonishment
  



More information about the Python-list mailing list