dynamic creating of class

Andrew Wilkinson ajw126 at NOSPAMyork.ac.uk
Sat Jun 21 15:01:39 CEST 2003


> #Python:
> class A:
> x = 'class A'
> class B:
> x = 'class B'
> kind = 'B'
> run = kind() # IS DOES NOT WORK :(
> print run.x

What you're trying to do here is call a string, which is silly - what you
actually want to do is get the object referred to by contents of the
string, and call that. The eval function is what you're looking for...

replace
run = kind()
with
run = eval(kind)()

and it'll work fine.

See http://www.python.org/doc/current/lib/built-in-funcs.html for more info.

HTH,
Andrew Wilkinson




More information about the Python-list mailing list