Keyword arguments - strange behaviour?

Alex Martelli aleaxit at yahoo.com
Fri Dec 24 03:36:04 EST 2004


<brian.bird at securetrading.com> wrote:
   ...
> I'm really looking for a neat way to do the following:
> 
> def method(a,b,opt1=None,opt2=None,opt3="",opt4=None):
> if opt1 is None: opt1=[]
> if opt2 is None: opt2={}
> if opt4 is None: opt4=[]
> 
> Python syntax is normally so neat but this just looks a mess if there
> are lots of parameters.

There's a decorator for that in the Cookbook (sorry, don't recall the
recipe number, but, it's on the site, and it got into the 2nd edition
which we're still expecting to have on paper at PyCon) -- basically
wrapping a function with a wrapper that does a copy.deepcopy on the
defaults values at each call (efficiency of course goes down the drain,
but you _did_ say "apart from efficiency"!-).


Alex



More information about the Python-list mailing list