I need to examine a python module to find simple data as well as “user defined objects”. Here, I mean objects (or functions or classes) that are not builtin, but defined with a class/def statement or dynamically loaded/generated.
Note there are similar questions, but they ask about “new style user defined classes”. I’ve seen the inspect
module – I couldn’t see how it could help.
I can import a module and ‘walk’ it, but I don’t know an easy way to identify an attribute as a simple type. eg – how do I tell 0. is a builtin type? here:
>>> a=0. >>> dir(a) ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__'... >>> type(a) <class 'float'>
Do I have to know the classes of all the standard objects to determine if an attribute of a module/object is “user defined”?
Advertisement
Answer
Try checking if the types module is builtin, usually works for me.
For example:
a = 1.2 type(a).__module__ == "__builtin__"