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 of stop
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…