How can the range function take either: a single argument, range(stop), or range(start, stop), or range(start, stop, step). Does it use a variadic argument like *arg to gather the arguments, and then use a series of if statements to assign the correct values depending on the number of arguments supplied? In essence, does range() specify that if there is one argument, then it set as the stop argument, or if there are two then they are start, and stop, or if there are three then it sets those as stop, start, and step respectively? I’d like to know how one would do this if one were to write range in pure CPython.
How can the range function take either: a single argument, range(stop) , or range(start,
Share
Range takes, 1, 2, or 3 arguments. This could be implemented with
def range(*args), and explicit code to raise an exception when it gets 0 or more than 3 arguments.It couldn’t be implemented with default arguments because you can’t have a non-default after a default, e.g.
def range(start=0, stop, step=1). This is essentially because python has to figure out what each call means, so if you were to call with two arguments, python would need some rule to figure out which default argument you were overriding. Instead of having such a rule, it’s simply not allowed.If you did want to use default arguments you could do something like:
def range(start=0, stop=object(), step=1)and have an explicit check on the type ofstop.