dynamic creating of class

Dialtone dialtone at despammed.com
Sat Jun 21 09:01:57 EDT 2003


Jarosław Zabiełło <webmaster at watchtowerDOTcom.pl> writes:

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

B is not a string, it's the name of the class you are calling.  with
kind=B you make a reference to class B and then you can call it.  using
kind='B' you just assign 'B' to kind but the string 'B' is just a string.

>>> class A:
...     x = 'class A'
...
>>> class B:
...     x = 'class B'
...
>>> kind = B
>>> run = kind()
>>> run.x
'class B'



-- 
try: troll.uses(Brain)
except TypeError, data: 
   troll.plonk()
Linux User #310274, Debian Sid Proud User




More information about the Python-list mailing list