7

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"?

effbiae
  • 1,087
  • 1
  • 7
  • 22
  • 1
    by your logic: when you use any framework they classes are `builtin` or user define? – Brown Bear Oct 22 '17 at 16:34
  • Could you give a precise definition of what you want to test for? "User-defined" and "built-in" are much more vague than you may realize. – user2357112 Oct 22 '17 at 16:38
  • @user2357112 I'm asking because the difference is so vague :) I'd like to walk over everything in bs4 (import bs4) and identify the 'interesting' objects/classes - differentiating the user defined classes under bs4 from classes that are not so interesting like the classes of float attributes in an object. – effbiae Oct 22 '17 at 16:46

1 Answers1

5

Try checking if the types module is builtin, usually works for me.

For example:

a = 1.2
type(a).__module__ == "__builtin__"
maor10
  • 1,645
  • 18
  • 28