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 xa*@xahlee.org
∑ http://xahlee.org/ 8 1473
On 5/13/05, Wolfram Kriesing <wo**************@gmail.com> wrote: 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
On 13 May 2005 02:52:34 -0700,
"Xah Lee" <xa*@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?
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/>
On Fri, 13 May 2005 11:52:34 +0200, Xah Lee <xa*@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
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)
> 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
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 xa*@xahlee.org
∑ http://xahlee.org/
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]
The last one already works: range(5,-4,-2)
[5, 3, 1, -1, -3]
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Penn Markham |
last post by:
Hello all,
I am writing a script where I need to use the system() function to call
htpasswd. I can do this just fine on the command line...works great
(see attached file, test.php). When my...
|
by: Chuck Anderson |
last post by:
I have a function with 7 inputs. The last three have default values.
I want to call that function specifying the first four, skip two and
then specify the last.
I thought I could write this...
|
by: domeceo |
last post by:
can anyone tell me why I cannot pass values in a setTimeout function
whenever I use this function it says "menu is undefined" after th
alert.
function imgOff(menu, num) {
if (document.images) {...
|
by: |
last post by:
How to call a function with variable argument list from another function
again with variable argument list?
Example :
double average ( int num, ... );
double AFunct1 ( int num, ... );
double...
|
by: laredotornado |
last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to
have my Javascript function execute from the BODY's "onload" method,
but if there is already an onload method defined, I would...
|
by: Robert Skidmore |
last post by:
Take a look at this new JS function I made. It is really simple but
very powerful. You can animate any stylesheet numeric value (top left
width height have been tested), and works for both % and px...
|
by: sfeher |
last post by:
Hi All,
Is there a way to preserve the arguments across functions?
I have:
<script>
function myFirstFunction()
{
// arguments = 'param1'
|
by: carvalho.miguel |
last post by:
hello,
imagine you have a static class method that receives a function
pointer, an int with the number of arguments and a variable number of
arguments.
in that static method you want to call...
|
by: Larax |
last post by:
Best explanation of my question will be an example, look below at this
simple function:
function SetEventHandler(element)
{
// some operations on element
element.onclick =
function(event)
{
|
by: VK |
last post by:
I was getting this effect N times but each time I was in rush to just
make it work, and later I coudn't recall anymore what was the original
state I was working around. This time I nailed the...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |