[Python-ideas] fixing mutable default argument values
Joel Bender
jjb5 at cornell.edu
Thu Jan 18 14:44:12 CET 2007
Calvin Spealman wrote:
> I dont understand how that would be different than doing
>
> c = c if c is not None else Bar([2,3,4])
>
Because that would be calling Bar(), perhaps creating a new Bar object,
every time foo() is called with None for c, which is not what a default
argument values are about. I'm proposing a way to create function local
singleton objects, removing them from the parameter list.
def foo(x):
local history = []
history.append(x)
Rather than:
def foo(x, history=[]):
history.append(x)
and then hoping that nobody calls foo() with a history parameter.
Joel
More information about the Python-ideas
mailing list