📅 2010-Jan-28 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ python, types ⬩ 📚 Archive
isinstance seems to be the preferred method 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
= 1
i = 0.1
f = "Hell"
s = [0, 1, 2]
l = {0:"Zero", 1:"One"}
d = (0, 1, 2)
t = True
b = None n
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:
is None n