dynamic creating of class

Marek "Baczek" Baczyński imbaczek at poczta.onet.pl
Sun Jun 22 18:05:36 EDT 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 :))

> #Python:
> 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()
or
# the ugly way, sometimes the only though
kind = 'B'
run = eval(kind)()

> print run.x

Voila.

Pozdrawiam, Baczek




More information about the Python-list mailing list