Does anyone know how I can test if an object's type is a builtin type or a user defined type ?

Recommended Answers

All 2 Replies

Does anyone know how I can test if an object's type is a builtin type or a user defined type ?

If it's not builtin, it must be imported from another module.

>>> getattr(list, '__module__')
'__builtin__'
>>>
commented: good idea +2
>>> getattr(list, '__module__')
'__builtin__'
>>>

Yes, it's a good idea, for the most basic types. In fact the question is ambiguous. Types defined in C extension modules are not detected with this method. For example

>>> from collections import deque
>>> print(deque.__module__)
collections

but in fact the deque type lives in a C library _collections.so . I don't know how to detect this automatically.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.