[Python] How to check if my type t is surrounded by typing.List or not? For example check the...

Discussão em 'Python' iniciado por Stack, Agosto 13, 2025.

  1. Stack

    Stack Membro Participativo

    I want to execute different code depending on the type of my t.

    I've got the follwing types:

    from pydantic import BaseModel
    from typing import List

    class Person(BaseModel):
    name: str
    id: int


    and

    t1 = Person
    t2 = List[Person]


    now I want to check if my variable tX is if of a list type or a normal type. How can I do that?

    def check_if_list(t):
    if type(t) == List: # <- this doesn't work how can i do this check
    print('LIST') # some code
    else:
    print('NO LIST') # some code


    t1 and t2 would give me:

    check_if_list(t1) # prints NO LIST
    check_if_list(t2) # prints LIST

    Continue reading...

Compartilhe esta Página