initialising a class by name

Willi Richert w.richert at gmx.net
Wed Jan 14 11:26:39 CET 2009


Hi,

try the following exemplarily for the os module

import os, types
[(c, klass) for (c,klass) in os.__dict__.items() if type(klass)==types.ClassType]

will print: [('_Environ', <class os._Environ at 0xb7d8114c>)]

Regards,
wr

Am Mittwoch, 14. Januar 2009 10:55:27 schrieb Krishnakant:
> On Wed, 2009-01-14 at 00:39 -0800, Chris Rebert wrote:
> > On Wed, Jan 14, 2009 at 12:36 AM, Krishnakant <krmane at gmail.com> wrote:
> > > On Wed, 2009-01-14 at 00:20 -0800, Chris Rebert wrote:
> > >> Aside from Steven's excellent idea, to use the getattr() technique
> > >> with your module scheme you'd probably also need to use __import__()
> > >> to dynamically import the right module.
> > >
> > > I would generally import all the modules I would need at the top of the
> > > main module.
> > > then would use getattr(module,class_name) will that work?
> >
> > Yes, that is how you'd do it in that case.
>
> By the way, is there a kind of global list of modules/classes which are
> maintained in a package once the program is loaded into memory?
> happy hacking.
> Krishnakant.
>
> > Cheers,
> > Chris
> > --
> > Follow the path of the Iguana...
> > http://rebertia.com
>
> --
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list