[Python] Why does range() in Python require positional arguments when called, but keyword...

Discussão em 'Python' iniciado por Stack, Fevereiro 9, 2025.

  1. Stack

    Stack Membro Participativo

    All examples tested using Python 3.13.2 on Windows 10.

    When calling range(), I must use positional arguments, or otherwise I get an exception.

    >>> range(2, 5)
    range(2, 5)
    >>> range(start=2, stop=5)
    Traceback (most recent call last):
    File "<python-input-1>", line 1, in <module>
    range(start=2, stop=5)
    ~~~~~^^^^^^^^^^^^^^^^^
    TypeError: range() takes no keyword arguments


    However, when trying to match pattern, it only works with keyword arguments instead.

    >>> match range(2, 5):
    ... case range(0, y, 1):
    ... print('1 argument')
    ... case range(x, y, 1):
    ... print('2 arguments')
    ... case range(x, y, z):
    ... print('3 arguments')
    ... case _:
    ... print('none of the above')
    ...
    Traceback (most recent call last):
    File "<python-input-0>", line 2, in <module>
    case range(0, y, 1):
    ~~~~~^^^^^^^^^
    TypeError: range() accepts 0 positional sub-patterns (3 given)
    >>> match range(2, 5):
    ... case range(start=0, stop=y, step=1):
    ... print('1 argument')
    ... case range(start=x, stop=y, step=1):
    ... print('2 arguments')
    ... case range(start=x, stop=y, step=z):
    ... print('3 arguments')
    ... case _:
    ... print('none of the above')
    ...
    2 arguments


    Interestingly, those arguments can't just use any arbitrary names. Using different ones causes the pattern match to fail, without raising an exception.

    >>> match range(2, 5):
    ... case range(aaa=0, bbb=y, ccc=1):
    ... print('1 argument')
    ... case range(aaa=x, bbb=y, ccc=1):
    ... print('2 arguments')
    ... case range(aaa=x, bbb=y, ccc=z):
    ... print('3 arguments')
    ... case _:
    ... print('none of the above')
    ...
    none of the above

    Continue reading...

Compartilhe esta Página