Python nested type check in runtime without external packages -
this question has answer here:
is there type checking api python3.5 typing can use check nested types in runtime. example:
from typing import list check_type([1,2,3], list[int]) # true check_type([1,2,''], list[int]) # false this example (it can 3 or more levels nested, have list, tuple, dict)
you can use all() built-in function below:
def check_type(iterable, tp): return all(isinstance(item, tp) item in iterable) output:
>>> check_type([1, 2, 3], int) true >>> >>> check_type([1, 2, ''], int) false
Comments
Post a Comment