The canonical way to check for the type of an object in Python is to use the isinstance function. Isinstance checks if an object is an instance of a specified class or of a subclass thereof.
Here's an example:
def check_type(obj): if isinstance(obj, int): return "Integer" elif isinstance(obj, float): return "Float" elif isinstance(obj, str): return "String" else: return "Other" print(check_type(42)) # Output: Integer print(check_type(3.14)) # Output: Float print(check_type("hello")) # Output: String print(check_type([1, 2, 3])) # Output: Other
Learn Flask development and learn to build cool apps with our premium Python course on Udemy.