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

Marco Herrn herrn at gmx.net
Wed Sep 10 15:32:02 EDT 2003


Hi,

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.
Thanks.

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. ;-)

Marco

-- 
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 gmx.net
(GnuPG/PGP-signed and crypted mail preferred)
Key ID: 0x94620736





More information about the Python-list mailing list