Why don't optional mutable objects show up in vars(func)?

Fredrik Lundh fredrik at pythonware.com
Wed Sep 27 12:52:37 EDT 2006


dannycolligan at gmail.com wrote:

> So I just got bitten by the "don't use a mutable object as an optional
> argument" gotcha.  I now realize that for this function:
> 
>>>> def func(x, y=[]):
> ...     y.append(x)
> ...     print y
> ...
> 
> y is initialized when the function is imported, not when the function
> is executed.  However, if this is the case, then why is y not showing
> up as an attribute of func?

 >>> func.func_defaults
([],)

</F>




More information about the Python-list mailing list