Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the strange arguments of range

The range function in python3 takes three arguments. Two of them are optional. So the argument list looks like:

[start], stop, [step]

This means (correct me if i'm wrong) there is an optional argument before a non-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 the built-in range.

like image 367
Kritzefitz Avatar asked Apr 08 '13 09:04

Kritzefitz


People also ask

How many arguments does range take?

The range function takes one or at most three arguments, namely the start and a stop value along with a step size.

Which argument of range () is not an optional argument it is required argument?

Below is the syntax of the range() function. It takes three arguments. Out of the three, two are optional. The start and step are optional arguments and the stop is the mandatory argument.

What is the range () function and what are its parameters?

The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.

What is the range () function give example?

One of the most common uses of the range() function is for iterating over a series of values in a for loop. This is particularly useful if you want to access each of the values in a list or array, or, for example, only every other value. In this example, the range() function is generating a sequence from 0 to 4 .


2 Answers

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:

def foo(*params):
    if 3 < len(params) < 1:
        raise ValueError('foo takes 1 - 3 arguments')
    elif len(params) == 1
        b = params[0]
    elif:
        a, b = params[:2]
    c = params[2] if len(params) > 2 else 1

but you could also just swap arguments:

def range(start, stop=None, step=1):
    if stop is None:
        start, stop = 0, start
like image 54
Martijn Pieters Avatar answered Sep 29 '22 02:09

Martijn Pieters


range does not take keyword arguments:

range(start=0,stop=10)
TypeError: range() takes no keyword arguments

it takes 1, 2 or 3 positional arguments, they are evaluated according to their number:

range(stop)              # 1 argument
range(start, stop)       # 2 arguments
range(start, stop, step) # 3 arguments

i.e. it is not possible to create a range with defined stop and step and default start.

like image 20
eumiro Avatar answered Sep 29 '22 01:09

eumiro