isinstance vs type(): The safer way to check Python types

isinstance vs type(): The safer way to check Python types

Is a Python value of a particular type? It’s tempting to say:

if type(s) == str:
    print('Yes, a string!')

Far better to say:

if isinstance(s, str):
    print('Yes, a string!')

Why?
– Works with subclasses
– Second argument can be a tuple of possibilities