dynamic creating of class
Marek "Baczek" Baczyński
imbaczek at poczta.onet.pl
Mon Jun 23 00:05:36 CEST 2003
On Sat, 21 Jun 2003 14:29:02 +0200, Jarosław Zabiełło wrote:
> Is there a possibility for Python to dynamic binding of class? I can
> do it in PHP but I do not know how to make the same in Python.
PHP is not Python. Python can act as PHP in many places, but it is
considered unpythonic (i.e. ugly :))
> class A:
> x = 'class A'
> class B:
> x = 'class B'
> kind = 'B'
> run = kind() # IS DOES NOT WORK :(
You're calling a string. What you really want is
# the pythonic way
kind = B # bind kind to object under the name B
run = kind()
# the ugly way, sometimes the only though
kind = 'B'
run = eval(kind)()
> print run.x
More information about the Python-list