Python: Checking Type of Variable
https://codeyarns.com/2010/01/28/python-checking-type-of-variable/
isinstance() seems to be the preferred way to check the type of a Python variable. It checks if the variable (object) is an instance of the class object being checked against.
# Variables of different types
i = 1
f = 0.1
s = "Hell"
l = [0, 1, 2]
d = {0:"Zero", 1:"One"}
t = (0, 1, 2)
b = True
n = None
All of the following return True:
isinstance(i, int) isinstance(f, float) isinstance(s, str) isinstance(l, list) isinstance(d, dict) isinstance(t, tuple)
True and False are an instance of bool, so:
isinstance(b, bool)
What about None? For that, there is always:
n is None
好的心态+正确的方法
浙公网安备 33010602011771号