function with variable arguments

Wolfram Kriesing wolfram.kriesing at gmail.com
Fri May 13 06:38:59 EDT 2005


using default args does actually solve it
what about

def Range(n, m=None, step=None)
  if step==None:
    if m==None:
      range(n)
    else:
      range(n,m)
  else:
    if m==None:
       raise Exception, "missing parameter m"
    else:
      range(n,m,step)

can be optimized i am sure :-)

-- 
cu

Wolfram


On 13 May 2005 02:52:34 -0700, Xah Lee <xah at xahlee.org> wrote:
> i wanted to define a function where the number of argument matters.
> Example:
> 
> def Range(n):
>     return range(n+1)
> 
> def Range(n,m):
>     return range(n,m+1)
> 
> def Range(n,m,step):
>     return range(n,m+1,step)
> 
> this obvious doesn't work. The default argument like
> Range(n=1,m,step=1) obviously isn't a solution.
> 
> can this be done in Python?
> 
> or, must the args be changed to a list?
> 
>  Xah
>  xah at xahlee.org
>http://xahlee.org/
> 
> --
> http://mail.python.org/mailman/listinfo/python-list


More information about the Python-list mailing list