Normally, if you have a function or method in Python and try to put non-default arguments before default arguments, it does not work.
#this will not work
def fake_range(start = 0, stop, step = 1):
while start < stop:
yield start
start += step
for i in fake_range(10)
: SyntaxError: non-default argument follows default argument
But for some reason, the range function behaves as if there are non-default arguments before the defaults
for i in range()
: TypeError: range expected 1 argument, got 0
for i in range(10)
: 0,1,2,3,4,5,6,7,8,9
for i in range(5, 10)
: 5,6,7,8,9
What is going on under the hood here? How come range works when I do range(stop)
or range(start, stop)
but this does not work with regular functions and methods?