Python >=3.5: Checking type annotation at runtime

前端 未结 3 1575
春和景丽
春和景丽 2020-12-29 04:59

Does the typing module (or any other module) exhibit an API to typecheck a variable at runtime, similar to isinstance() but understanding the type

3条回答
  •  醉梦人生
    2020-12-29 05:56

    I was looking for something similar and found the library typeguard. This can automatically do runtime type checks wherever you want. Checking types directly as in the question is also supported. From the docs,

    from typeguard import check_type
    
    # Raises TypeError if there's a problem
    check_type('variablename', [1234], List[int])
    

提交回复
热议问题