[Python] Difference between TypeVar('T') and Any

Discussão em 'Python' iniciado por Stack, Junho 23, 2025.

  1. Stack

    Stack Membro Participativo

    Assuming I have some generic function:

    def foo(*args):
    for arg in args:
    print(arg)


    If I wanted to type hint *args, according to the PEP I only need to specify the type one arg.

    What if the arguments can be of any type? What is the difference between:

    T = typing.TypeVar('T')

    def foo(*args: T):
    ...


    and

    def foo(*args: Any):
    ...


    To my knowledge, TypeVar('T') equates to "any type", but does that apply when variable args are of multiple types?

    Continue reading...

Compartilhe esta Página