Best way to enumerate classes in a module

  • Thread starter Дамјан ГеоргиевÑки
  • Start date
Ð

Дамјан ГеоргиевÑки

I need to programmaticaly enumerate all the classes in a given module.
Currently I'm using dir(module) but the Notice on the documentation page
[1] says "dir() is supplied primarily as a convenience for use at an
interactive prompt" so that kind of scares me.

Is there a better approach?

If there is, how do I get all the classes of the current module?


[1] http://docs.python.org/library/functions.html#dir
 
P

Peter Otten

Дамјан ГеоргиевÑки said:
I need to programmaticaly enumerate all the classes in a given module.
Currently I'm using dir(module) but the Notice on the documentation page
[1] says "dir() is supplied primarily as a convenience for use at an
interactive prompt" so that kind of scares me.

Is there a better approach?

If there is, how do I get all the classes of the current module?

inspect.getmembers(module, inspect.isclass), but this uses dir() internally.
You may have to remove imported classes:
.... def accept(obj):
.... return inspect.isclass(obj) and module.__name__ ==
obj.__module__
.... return [class_ for name, class_ in inspect.getmembers(module,
accept)]
....[<class 'inspect.ArgInfo'>, <class 'inspect.ArgSpec'>, ...]

Peter
 
C

Carl Banks

I need to programmaticaly enumerate all the classes in a given module.
Currently I'm using dir(module) but the Notice on the documentation page
[1]  says "dir() is supplied primarily as a convenience for use at an
interactive prompt" so that kind of scares me.

Is there a better approach?

If there is, how do I get all the classes of the current module?


You can use module.__dict__.values() (or .itervalues()) to retrieve
the contents of the module (and of course .keys() if you want names).
If you want to check the same module that the code appears in, use
globals() instead of module.__dict__.

Something makes me think that module.__dict__ was only added to Python
fairly recently, but I'm not sure.

A word of warning (although I would guess you are already aware of
these issues, but for other readers): this method can't tell the
difference between a class defined in the module and a class imported
into it.

Finally, despite the warning, I think you are ok to use dir() for that
purpose. It's not likely to change.


Carl Banks
 
T

Terry Reedy

Дамјан ГеоргиевÑки said:
I need to programmaticaly enumerate all the classes in a given module.
Currently I'm using dir(module) but the Notice on the documentation page
[1] says "dir() is supplied primarily as a convenience for use at an
interactive prompt" so that kind of scares me.

That notice primarily refers to the fact that the special names (of
__xxx__ form) returned are implementation and version dependent, and may
not be complete in the sense that dir(ob) may not return __xyz__ even
though ob.__xyz__ exists and can be retrieved.

If you are only interested in regular-name attribute of ob, let your
fear fly away.
 
S

Steven D'Aprano

It exists in python2.1 - I don't have an older python to check at the
moment.

$ python1.5
Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat
4.1.2-27)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam27
 
M

Michele Simionato

Дамјан ГеоргиевÑки said:
I need to programmaticaly enumerate all the classes in a given module.
Currently I'm using dir(module) but the Notice on the documentation page
[1]  says "dir() is supplied primarily as a convenience for use at an
interactive prompt" so that kind of scares me.

That notice primarily refers to the fact that the special names (of
__xxx__ form) returned are implementation and version dependent, and may
not be complete in the sense that dir(ob) may not return __xyz__ even
though ob.__xyz__ exists and can be retrieved.

If you are only interested in regular-name attribute of ob, let your
fear fly away.

There are rather special cases (not your case) where dir does not
retrieve
all the names to which an object can respond. In particular this
happens
for class objects:
In [1]: class C(object): pass
...:

In [2]: dir(C)
Out[2]:
['__class__',
'__delattr__',
'__dict__',
'__doc__',
'__getattribute__',
'__hash__',
'__init__',
'__module__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__str__',
'__weakref__']


Notice the absense of the special names __name__, __mro__,
__subclasses__ and also mro, which
is not special. All these names are defined on the metaclass type and
C responds to them,
but they are not retrieved by dir.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top