How to check if a variable’s type is primitive?

Since there are no primitive types in Python, you yourself must define what you consider primitive:

primitive = (int, str, bool, ...)

def is_primitive(thing):
    return isinstance(thing, primitive)

But then, do you consider this primitive, too:

class MyStr(str):
    ...

?

If not, you could do this:

def is_primitive(thing):
    return type(thing) in primitive

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)