function with variable arguments
Peter Dembinski
pdemb at gazeta.pl
Fri May 13 07:39:26 EDT 2005
On Fri, 13 May 2005 11:52:34 +0200, 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?
It can be written this way:
def Range_3args(n, m, step):
return range(n, m + 1, step)
def Range_2args(n, m):
return range(n, m + 1)
def Range(n, m = None, step = None):
if (m is None) and (step is None):
return range(n + 1)
if (not (m is None)) and (step is None):
return Range_2args(n, m)
if (not (m is None)) and (not (step is None)):
return Return_3args(n, m, step)
return []
--
http://www.peter.dembinski.prv.pl
More information about the Python-list
mailing list