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

Nick Welch mack at incise.org
Tue Sep 9 16:48:11 EDT 2003


On Tue, Sep 09, 2003 at 01:35:37PM -0700, Chad Netzer wrote:
> "class" is a keyword, and so the above won't work, but the idea is
> sound. (Just use a different identifier than 'class')

Doh, totally slipped my mind.

> The first form, using getattr, is perfectly idiomatic, and that is what
> I use.

Agreed, getattr isn't bad, but using vars()[foo] or globals()[foo]
always feels gross to me.

> Also, regarding the earlier import question, there is the builtin
> __import__ function:
> 
> help(__import__)

Cool.  I've seen it before but never gave any thought to it.

-- 
Nick Welch aka mackstann | mack @ incise.org | http://incise.org
Every man has his price.  Mine is $3.95.





More information about the Python-list mailing list