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).
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:but you could also just swap arguments:
range
does not take keyword arguments:it takes 1, 2 or 3 positional arguments, they are evaluated according to their number:
i.e. it is not possible to create a range with defined
stop
andstep
and defaultstart
.