make a class instance from a string ?

Diez B. Roggisch deets at nospam.web.de
Thu Feb 23 09:23:15 EST 2006


Luke Plant wrote:

> Bo Yang wrote:
> 
>> I know in java , we can use
>>
>> class.ForName("classname")
>>
>>
>> to get an instance of the class 'classname' from a
>> string , in python , how do I do that ?
> 
> In Python, classes are first class objects, so normally you would pass
> the class itself around, rather than use the names of classes.  Of
> course that might not be practical or applicable in your situation.

While JAVA is severely limited regarding the number of seats in the first
class, classes _are_ sitting there.

The need for dynamic attribute look up is even more frequent in python -
think getattr, __getitem__, __getattr__.

Diez



More information about the Python-list mailing list