How to instatiate a class of which the name is only known at runtime?

Marco Herrn herrn at
Wed Sep 10 21:32:02 CEST 2003


On Tue, Sep 09, 2003 at 01:35:37PM -0700, Chad Netzer wrote:
> On Tue, 2003-09-09 at 12:45, Nick Welch wrote:
> > Although this all seems really dirty to me.  Maybe there are better ways
> > to do it.
> The first form, using getattr, is perfectly idiomatic, and that is what
> I use.
> Also, regarding the earlier import question, there is the builtin
> __import__ function:

Yes! That is exactly what I was searching for. That helps a lot to me.

I already searched for everything that has anything to do with modules,
but I never ran across __import__(). It seems that I am searching not
effectively enough. I am not very familiar with python, so can you give
some tips on how to search for functions or modules to use, when I have
a specific problem to solve? What I mean is, you have helped me, but
without this list, I would have never found this solution. At what
places should one look, when he is searching for existing functions or
language features or the "python way" doing some things, aside from this
list of course. ;-)


The years of peak mental activity are undoubtedly between the ages of
four and eighteen.  At four we know all the questions, at eighteen all
the answers.

Marco Herrn             herrn at
(GnuPG/PGP-signed and crypted mail preferred)
Key ID: 0x94620736

More information about the Python-list mailing list