2
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by Gribouillis
1

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__'
>>>
Votes + Comments
good idea
0
>>> 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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.