Bug in python!? persistent value of an optional parameter in function!

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Thu Mar 8 03:12:52 EST 2007


C Barr Leigh a écrit :
> Help! Have I found a serious bug?

No. This is a FAQ. Default arguments of functions are evaled only once - 
when the def statement is eval'd and the function object constructed.

> This seems like highly undesired behaviour to me.

Possibly, but this is unlikely to change in a near future, so you'd 
better get used to it. If you want mutable objects as default values for 
functions, the idiom is:

def some_func(default=None):
   if default is None:
     default = []
   # code here

HTH



More information about the Python-list mailing list