Velocity Reviews > function with variable arguments

# function with variable arguments

Xah Lee
Guest
Posts: n/a

 05-13-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://xahlee.org/

Steve
Guest
Posts: n/a

 05-13-2005
On 5/13/05, Wolfram Kriesing <(E-Mail Removed)> wrote:
> using default args does actually solve it
> def Range(n, m=None, step=None)
> if step==None:
> if m==None:
> range(n)
> else:

<...snip...>
.....or better still :

def Range(*args):
return range(*args)

Regards
Steve

PS: but what do I know, I'm a F'ing imcompetent ass

Dan Sommers
Guest
Posts: n/a

 05-13-2005
On 13 May 2005 02:52:34 -0700,
"Xah Lee" <(E-Mail Removed)> 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?

Assuming you're doing something more interesting than wrapping range:

def Range( start, stop = None, step = 1 ):
if stop == None: # i,e., we only got one argument
stop = start
start = 1
# rest of function goes here....

HTH,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>

Peter Dembinski
Guest
Posts: n/a

 05-13-2005
On Fri, 13 May 2005 11:52:34 +0200, Xah Lee <(E-Mail Removed)> 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

PoD
Guest
Posts: n/a

 05-13-2005
On Fri, 13 May 2005 02:52:34 -0700, Xah Lee 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?
>

def Range(n,m=None,step=1):
if m is None:
n,m = 0,n+1
else:
n,m = n,m+1
return range(n,m,step)

Wolfram Kriesing
Guest
Posts: n/a

 05-13-2005
> def Range(n,m=None,step=1):
> if m is None:
> n,m = 0,n+1
> else:
> n,m = n,m+1
> return range(n,m,step)

i like this one.

coming from php (just a couple weeks ago) its always again interesting
to see how i have to start thinking to program differently, it can be
so much easier with python. i dont want to go back to php!

--
cu

Wolfram

Xah Lee
Guest
Posts: n/a

 05-14-2005
Thanks to all for the reply. (i should've known better)

on a related topic,
I think it would be a improvement for the built-in range() so that step
needs not be an integer.
Further, it'd be better to support decreasing range. e.g.

Range( 5, 7, 0.3); # returns [5, 5.3, 5.6, 5.9, 6.2, 6.5, 6.8]
Range( 5, -4, -2); # returns [5,3,1,-1,-3]

Xah
(E-Mail Removed)
http://xahlee.org/

tiissa
Guest
Posts: n/a

 05-14-2005
Xah Lee wrote:
> on a related topic,
> I think it would be a improvement for the built-in range() so that step
> needs not be an integer.

There are easy workarounds but I'd find it useful as well.

> Further, it'd be better to support decreasing range. e.g.
>
> Range( 5, 7, 0.3); # returns [5, 5.3, 5.6, 5.9, 6.2, 6.5, 6.8]
> Range( 5, -4, -2); # returns [5,3,1,-1,-3]

>>> range(5,-4,-2)

[5, 3, 1, -1, -3]

Harald Schmidt
Guest
Posts: n/a

 05-14-2005
Xah Lee wrote:

> I think it would be a improvement for the built-in range() so that step
> needs not be an integer. [...]
>
> Range( 5, 7, 0.3); # returns [5, 5.3, 5.6, 5.9, 6.2, 6.5, 6.8]

This may not return what you expect it to return.

For example let's use a naive implementation like this:

def Range(start, stop, step):
values = []
while start < stop:
values.append(start)
start += step
return values

The result is:
>>> Range(5, 7, 0.3)

[5, 5.2999999999999998, 5.5999999999999996, 5.8999999999999995,
6.1999999999999993, 6.4999999999999991, 6.7999999999999989]

Worse: Range(5, 7.1, 0.3) would return 8 values, not 7 as expected from e.g.
range(50, 71, 3).

Welcome to the interesting world of floating point numbers.

Harald