function with variable arguments

Discussion in 'Python' started by Xah Lee, May 13, 2005.

  1. Xah Lee

    Xah Lee Guest

    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://xahlee.org/
     
    Xah Lee, May 13, 2005
    #1
    1. Advertising

  2. Xah Lee

    Steve Guest

    On 5/13/05, Wolfram Kriesing <> 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
     
    Steve, May 13, 2005
    #2
    1. Advertising

  3. Xah Lee

    Dan Sommers Guest

    On 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?


    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/>
     
    Dan Sommers, May 13, 2005
    #3
  4. On Fri, 13 May 2005 11:52:34 +0200, 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?


    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
     
    Peter Dembinski, May 13, 2005
    #4
  5. Xah Lee

    PoD Guest

    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)
     
    PoD, May 13, 2005
    #5
  6. > 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
     
    Wolfram Kriesing, May 13, 2005
    #6
  7. Xah Lee

    Xah Lee Guest

    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

    ∑ http://xahlee.org/
     
    Xah Lee, May 14, 2005
    #7
  8. Xah Lee

    tiissa Guest

    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]
     
    tiissa, May 14, 2005
    #8
  9. 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
     
    Harald Schmidt, May 14, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    10
    Views:
    692
    sushant
    Jan 20, 2005
  2. tutmann
    Replies:
    4
    Views:
    454
  3. jmborr
    Replies:
    1
    Views:
    446
    Stargaming
    Nov 3, 2007
  4. Replies:
    3
    Views:
    851
  5. Navaneeth
    Replies:
    4
    Views:
    571
    Kenny McCormack
    Nov 20, 2010
Loading...

Share This Page