Python style advice

Nick Craig-Wood ncw at axis.demon.co.uk
Tue Feb 10 05:30:02 EST 2004


Wayne Folta <wfolta at netmail.to> wrote:
>  In the specific case where you're trying to map a variable-length 
>  argument list to local variables in a function, you can do the more 
>  Pythonic:
> 
>  def myfunc (a = None, b = None, c = None)

Interesting...  I was thinking in particular of sys.argv with this
example, but the above gave me the idea below which would work quite
well (imagine sys.argv in place of L below)

>>> def f(a=None,b=None,c=None,*extra): print a,b,c
... 
>>> L=[]; apply(f,L)
None None None
>>> L=[1]; apply(f,L)
1 None None
>>> L=[1,2]; apply(f,L)
1 2 None
>>> L=[1,2,3]; apply(f,L)
1 2 3
>>> L=[1,2,3,4]; apply(f,L)
1 2 3
>>> L=[1,2,3,4,5]; apply(f,L)
1 2 3

-- 
Nick Craig-Wood
ncw at axis.demon.co.uk



More information about the Python-list mailing list