get a list of classes in this module

Terry Reedy tjreedy at udel.edu
Wed Jun 12 16:34:53 EDT 2002


"Mark McEahern" <mark at mceahern.com> wrote in message
news:mailman.1023907851.3426.python-list at python.org...
> Suppose I have a list of classes:
>
> # validation.py
> class CreditCardValidation:
...
> and I want to make a helper method in the module to translate card
type
> strings into their appropriate class constructor.
...
> I know I can use inspect.getmembers() to do this, but what if I want
to put
> the helper method in the same file as the classes (i.e.,
validation.py)?
> How do I refer to *this* module?

I believe the name of a modulue is the same inside as out.  I do not
know inspect or whether it can work on a module under construction.
However, if the subclasses are too numerous to build the dict by hand,
or the set so dynamic that you do not trust yourself to update it
properly, I believe something like the following (untested) should
work:

def propersubclasstuple((classname,classobj)): return <boolean
expression>

  class_dict = dict(filter(propersubclasstuple, globals().items() ))

> Should I use globals()?  I mean, that would work--so I guess the
question is
> whether there's a more Pythonic approach?

Correct and simple (in that order) *is* Pythonic ;<)

Terry J. Reedy






More information about the Python-list mailing list