[Python-ideas] my take on mutable default arguments

tomer filiba tomerfiliba at gmail.com
Sat Jan 27 23:22:32 CET 2007

On 1/28/07, Chris Rebert <cvrebert at gmail.com> wrote:
> As pointed out by Calvin Spealman in an earlier email:
> Calvin Spealman wrote:
>  > Deep copy of the originally created
>  > default argument can be expensive and would not work in any useful way
>  > with non-literals as defaults, such as function calls or subscript
>  > lookups or even simple attributes.

first, please refer to my second version (@copydefaults).

second, theoretically all objects are copyable, as long as they
define __copy__ or __deepcopy__. it's up to you.

third, you'll use this decorator only when you want "fresh copies" of the
default values... not "just for fun", and since you'll need these fresh copies
anyhow, creating the value by hand ("if x is None: x = []") will not yield
a dramatic performance gain, as most default objects are "primitive"
anyway (empty lists and stuff, not dicts of 10000 keys)

the main issue is readability and auto-documentation. using
"def f(x = [])" is better documenting than "def f(x = None)"

and fourth, my apologies, but such a decorator already existed (and even
with the same semantics that i used):


More information about the Python-ideas mailing list