In Python, I understand that default arguments come at the end and that non-default arguments cannot follow a default argument. That is fine. Like for example:
>>> def foo(x=0, y):
return x, y
SyntaxError: non-default argument follows default argument
That is OK as expected.
However, what about the case when I want that the first argument should be a default one? Like for example, as is apparent from the above code, x
has to be the first argument and it should have a default value of 0.
Is it possible to do this? I am asking because even in the range
function, I am guessing it is something like this:
def range(start=0, end):
pass
So how is this done and if it is not possible, how is this implemented by range
? Note that I am insisting on the first argument to be default, that is the entire point. I am using range
as an example because it fits my problem perfectly. Of course one could implement range
as def range(end, start=0)
, but that is not the point.
The order of the default argument will be random.
The error “SyntaxError: non-default argument follows default argument” occurs when you specify a default argument before a non-default argument. To solve this error, ensure that you arrange all arguments in your function definition such that the default arguments come after the non-default arguments.
Notice that when keyword arguments are used in the call, the order in which arguments are listed doesn't matter; Python matches by name, not position.
The Python "SyntaxError: non-default argument follows default argument" occurs when we define a function with a positional parameter that follows a default parameter. To solve the error, make sure to specify all default parameters after the positional parameters of the function.
Well, range
is C code which can do this slightly better. Anyways, you can do this:
def range(start, stop=None):
if stop is None: # only one arg, treat stop as start ...
stop = start
start = 0
...
and document the function accordingly.
There are a couple approaches. The first would be to switch the arguments in the function, if some of the arguments are "None". That would work like this.
def range1(value, end=None):
if end == None:
end = value
value = 0
return _generate_range_values(value, end)
The other primary method would be to have your function get a list of all arguments it receives. Then it can decide what to do, based on the number of arguments.
def range2(*args):
if len(args) == 1:
start = 0
end = int(args[0])
elif len(args) == 2:
start = int(args[0])
end = int(args[1])
return _generate_range_values(start, end)
The third would be to encourage users to pass named arguments to your function, which makes the order less important.
def range3(end, start=0):
return _generate_range_values(start, end)
Then users would call it with the named start argument when they wanted something besides 0. (Although the named argument would not be required, it keeps the code clear.
for i in range3(55, start=12)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With