How to pass a dictionary to the range() function

Asked

Viewed 79 times

4

I was looking at the documentation (docstring) of the range function and came across the following:

Init signature: range(self, /, *args, **kwargs)
Docstring:     
range(stop) -> range object
range(start, stop[, step]) -> range object

My understanding is that the function range() takes both positional arguments (a tuple or list) and keyword arguments (dictionary).

Testing, I managed to do the following:

# passando argumentos da forma padrão:
for i in range(0, 10, 1):
    print(i)

Returns sequence 1 to 10 with step 1.

# passando argumentos como uma lista:
for i in range(*[1, 10, 2]):
    print(i)

Using the * as an "unpacking" indicator, the range resolves and returns an iterable with: 1, 3, 5, 7, 9.

And if I wanted to pass a dictionary, as below?

for i in range(**{'start':0, 'stop':100, 'step':10}):
    print(i)

While executing the above code I take an error:

Typeerror: range() takes no keyword argumentsrangeRange

It’s a docstring error, or you don’t even have a way to pass **kwargs for the range?


EDIT: version and docstring inserir a descrição da imagem aqui

  • In which version of Python did you see this? As far as I know this is not possible. In time in version 3.8.1 the docstring is different

  • There’s no way to do it, but idle people always invent their tricks for i in range(*{'start':0, 'stop':100, 'step':10}.values()): print(i)

  • @Augusto Vasques, in this case, what is passed to the range is a tuple, generated from the dictionary. There is guarantee that the values brought by the method dict.values() will always be in the order they were declared?

  • 1

    Modified in version 3.7: Dictionary order is guaranteed according to insertion order. This behavior was a detail of Cpython’s implementation from version 3.6 onwards. Ref: https://docs.python.org/pt-br/3/library/stdtypes.html#Mapping-types-Dict , but still a gambiarra as it is verbose code for a simple operation.

  • @Paulomarques, the version I saw this docstring was in 3.9.1

1 answer

6


This happens because the function range is not implemented directly in Python, but rather in C. In this case, the C function has been developed to accept only the arguments in positional form.

Although, in the case of range, is shown by the documentation the argument names start, stop and step, they cannot be passed through the form named on account of the said limitation.

Of language reference:

Cpython implementation Detail: An implementation may provide built-in functions Whose positional Parameters do not have Names, Even if they are ːnamed' for the purpose of Documentation, and which therefore cannot be supplied by keyword. In Cpython, this is the case for functions implemented in C that use PyArg_ParseTuple() to parse their Arguments.


This response was based in this question (and answers) soen.

  • 3

    This - also note that the "/" signature was included in Python 3.8 (or 3.7 - I can’t remember) to allow "positional only" parameters in pure Python - but because of which argument it turns which parameter to change with the number of arguments in the range, docstring does not respect this convention

  • 2

    Another thing uqe is worth mentioning is that range, as well as other builtins like "int" and "str", although it "looks" like a function, is a class - the Python syntax makes us use these callers ("callable" in English) without worrying whether they are classes or functions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.