Default parameter for a method

John Nagle nagle at animats.com
Wed Apr 16 16:21:46 EDT 2008


s0suk3 at gmail.com wrote:
> I wanted to know if there's any way to create a method that takes a
> default parameter, and that parameter's default value is the return
> value of another method of the same class. For example:
> 
...

> 
>     def meth2(self, arg=meth1()):

    Not good.  If the default value of an argument is mutable, there
are wierd effects, because the default value is bound once when the
class is created, then shared between all later uses.  This is almost
never what was wanted or intended, and it's a common source of subtle
bugs.

    In general, default values should be immutable constants only.
There's been talk of fixing this (it's really a design bug in Python),
but for now, it's still broken.

    (I just had horrible thoughts about the implications of binding
a closure to a default argument.  You don't want to go there.)

				John Nagle



More information about the Python-list mailing list