import modules at runtime
emile at fenx.com
emile at fenx.com
Mon May 8 11:26:01 EDT 2000
You could also play with changing the instance's class or bases
attributes, e.g.:
>>> class T1:
def __repr__(self):
return "T1"
>>> class P1:
def __repr__(self):
return "P1"
>>> a = P1()
>>> a
P1
>>> a.__class__ = T1
>>> a
T1
--- or try things like:
>>> class T3:
def __str__(self):
return "T3"
>>> a.__class__.__bases__=(T3,)
>>> a
T1
>>> print a
T3
Emile van Sebille
emile at fenx.com
Alex <cut_me_out at hotmail.com> wrote in message
news:<etdg0rtm6jo.fsf at w20-575-109.mit.edu>...
>
> > Is there a way to create an object instance giving its classname at
> > runtime?
>
> This sort of thing works:
>
> >>> class C: pass
> ...
> >>> c = eval ('C') ()
> >>> c
> <__main__.C instance at 103e18>
> >>>
>
> Alex.
> --
> http://www.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list