Re: function with variable arguments
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 [EMAIL PROTECTED] http://xahlee.org/ -- http://mail.python.org/mailman/listinfo/python-list
Re: function with variable arguments
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] -- http://mail.python.org/mailman/listinfo/python-list
Re: function with variable arguments
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.2998, 5.5996, 5.8995, 6.1993, 6.4991, 6.7989] 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 -- http://mail.python.org/mailman/listinfo/python-list
function with variable arguments
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 [EMAIL PROTECTED] http://xahlee.org/ -- http://mail.python.org/mailman/listinfo/python-list
Re: function with variable arguments
On 13 May 2005 02:52:34 -0700, Xah Lee [EMAIL PROTECTED] 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/ -- http://mail.python.org/mailman/listinfo/python-list
Re: function with variable arguments
On Fri, 13 May 2005 11:52:34 +0200, Xah Lee [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: function with variable arguments
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) -- http://mail.python.org/mailman/listinfo/python-list
Re: function with variable arguments
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 -- http://mail.python.org/mailman/listinfo/python-list