the strange arguments of range

2020-02-05 13:50发布

问题:

The range function in python3 takes three arguments two of them are optional. So the argumentlist looks like:

[start], stop, [step]

so this means (correct me if i'm wrong) there is a optional argument before an not-optional argument. But if i try to define a function like this i get this:

>>> def foo(a = 1, b, c = 2):
    print(a, b, c)
SyntaxError: non-default argument follows default argument

is this something i can't do as a 'normal' python user, or can i somehow define such a function? Of course i could do something like

def foo(a, b = None, c = 2):
    if not b:
        b = a
        a = 1

but for example the help function would then show strange informations. So i really want to know if it's possible do define a function like above (the first one).

回答1:

range() takes 1 positional argument and two optional arguments, and interprets these arguments differently depending on how many arguments you passed in.

If only one argument was passed in, it is assumed to be the stop argument, otherwise that first argument is interpreted as the start instead.

In reality, range(), coded in C, takes a variable number of arguments. You could emulate that like this:

def foo(*params):
    if 3 < len(params) < 1:
        raise ValueError('foo takes 1 - 3 arguments')
    elif len(params) == 1
        b = params[0]
    elif:
        a, b = params[:2]
    c = params[2] if len(params) > 2 else 1

but you could also just swap arguments:

def range(start, stop=None, step=1):
    if stop is None:
        start, stop = 0, start


回答2:

range does not take keyword arguments:

range(start=0,stop=10)
TypeError: range() takes no keyword arguments

it takes 1, 2 or 3 positional arguments, they are evaluated according to their number:

range(stop)              # 1 argument
range(start, stop)       # 2 arguments
range(start, stop, step) # 3 arguments

i.e. it is not possible to create a range with defined stop and step and default start.



回答3:

def foo(first, second=None, third=1):
     if second is None:
         start, stop, step = 0, first, 1
     else:
         start, stop, step = first, second, third