Create object name from string value?

Adam Tauno Williams awilliam at opengroupware.us
Thu Jan 21 11:12:22 EST 2010


> > but for the record, the way to use exec is like this:
> > exec("object1 = classname()")
> I failed to make that work. So back to the original question. How to
> make an instance named according to a string inside a variable? I
> guess it should be in the top-level namespace, not inside a list or
> dictionary.

I think what you want is more like:

classclass = eval(classname)
x = classclass(...args...)

<http://coils.hg.sourceforge.net/hgweb/coils/coils/file/2c7847ef0527/src/coils/protocol/dav/davroot.py>

-- 
OpenGroupware developer: awilliam at whitemice.org
<http://whitemiceconsulting.blogspot.com/>
OpenGroupare & Cyrus IMAPd documenation @
<http://docs.opengroupware.org/Members/whitemice/wmogag/file_view>




More information about the Python-list mailing list