[Tutor] newbie question about default arguments

Josh Adams jadams at iastate.edu
Wed Mar 29 20:21:29 CEST 2006


Thanks for your help.  That makes a lot more sense.  

Not to ask too many stupid questions, but why does the L2 assignment in the
if-block create a new L variable?  Shouldn't the scope from the function
definition dominate the inner scope of the if-block?

Thanks,
Josh

> Josh,
> 
> If you print the id() of your L inside your f2(), you will notice something..
> In short, the default value stays the same; what you modified was another
> copy of []. Hope it helps.
> 
> def f2(a, L=[]):
>       print "id(L) = ", id(L)
>       if L==[]:
>             L=[]
>             print "id(L2) =", id(L)
>       L.append(a)
>       return L
> 
> >>> print f2(1)
> id(L)= 11788336
> id(L2)= 12047184
> [1]
> 
> Kenny
> 
> 





More information about the Tutor mailing list