First of all, in Python there is no difference between classes and types:
>>> type("234"), "234".__class__ (str, str)
Then type testing for objects can have two different values:
isinstance checks if your object has a given type or subtype:
>>> class mystr(str): pass >>> s = mystr(4) >>> s '4' >>> ype(s) __main__.mystr >>> isinstance(s, str) True
then
>>> type(s) is str False >>> type(s) <class '__main__.mystr'>
by the way, you should not write type(s) == str , but type(s) is str .
Now you can answer your question: __builtin__ built-in type module
>>> str.__module__ '__builtin__' >>> mystr.__module__ '__main__'
So you can probably write
>>> def is_of_builtin_type(obj): return type(obj).__module__ == '__builtin__' >>> is_of_builtin_type("4223") True >>> class mystr(str): pass >>> is_of_builtin_type(mystr(223)) False
Note. I have not tested how safe it is.
source share