FAQ

On 20 Aug 2003 01:15:44 -0700, nick.keighley at marconi.com (Nick Keighley) wrote:

I'd like to generate a class on-the-fly from a parameter (a dictionary).
class MyClass(object):
def __init__(self, deadparrot):
self.deadparrot = deadparrot
mydict = { 'MyClass' : 'foobar'} # { className : constructorArgument }

className = mydict.keys()[0]
arg = mydict.values()[0]

myInstance = eval(className + '(arg)')
myInstance
<__main__.MyClass object at 0x00A7DE30>
myInstance.deadparrot
'foobar'

HTH,

Daniel Klein
Member of the Dead Parrot Society

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 5 of 7 | next ›
Discussion Overview
grouppython-list @
categoriespython
postedAug 20, '03 at 8:15a
activeAug 22, '03 at 12:07p
posts7
users6
websitepython.org

People

Translate

site design / logo © 2022 Grokbase