[Python] Python 3 "TypeError: Cannot instantiate typing.Final" [duplicate]

Discussão em 'Python' iniciado por Stack, Abril 8, 2025.

  1. Stack

    Stack Membro Participativo

    When "TypeError: Cannot instantiate typing.*" appears, it seems to be about using parentheses instead of brackets. Still, I'm curious about why Python exhibits the following behavior:

    from typing import Final

    def f():
    h : Final[int] = 10 # Accepted
    i : Final(int) = 15 # Accepted

    f() # Ran successfully
    j : Final[int] = 1 # Accepted
    k : Final(int) = 2 # ERROR


    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python3.9/typing.py", line 335, in __call__
    raise TypeError(f"Cannot instantiate {self!r}")
    TypeError: Cannot instantiate typing.Final


    Why is it either-or inside function definitions, but not the main block?

    Continue reading...

Compartilhe esta Página