function with variable arguments

X

Xah Lee

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
(e-mail address removed)
∑ http://xahlee.org/
 
S

Steve

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:
<...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
 
D

Dan Sommers

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
 
P

Peter Dembinski

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 []
 
P

PoD

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)
 
W

Wolfram Kriesing

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!
 
X

Xah Lee

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 address removed)
∑ http://xahlee.org/
 
T

tiissa

Xah said:
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]

The last one already works: [5, 3, 1, -1, -3]
 
H

Harald Schmidt

Xah said:
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:[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top