[Python-Dev] Dynamic class inheritance && something else
Guido van Rossum
gvanrossum at gmail.com
Tue Jun 14 18:38:31 CEST 2005
On 6/14/05, Vero <aqua_azul_2000 at yahoo.com.mx> wrote:
> Hi. My name is Veronica, I am a master student at UNAM. I am working on
> something related to Artificial Inteligence and I have been looking for the
> most appropriated programming language to implement my algorithms. I found
> python to be very close to what I need, but there are still a couple of
> details that I haven't been able to solve.
>
> First, (and most important) I would like to be able to dynamically modify
> the classes from which a class inherits. I haven't found any way to do it
> with the language as it is. If there is a way, any suggestion will be truly
> appreciated. If I had to modify the source code to achieve this, I hope
> that you could give me some hints; I have an idea of how something like this
> could be achieved but since I don't know deeply the python sourcode I could
> get lost.
I wonder if assignment to __class__ (for instances) or to __bases__
(for classes) is what you are looking for?
>>> class C:
def foo(self): print "foo"
>>> class D:
def foo(self): print "D.foo"
>>> x = C()
>>> x.__class__ = D
>>> x.foo()
D.foo
>>> x.__class__ = C
>>> x.foo()
foo
>>>
> Second, since my program will be modifying classes during run time, I would
> like to have a way to write on a file the python code that would have
> defined the class with the functions and attributes as they were left, just
> as if it had been writen like that at the very begining. I need it to be
> python code because I will be using that latter. Maybe I will have to solve
> this second problem by myself but I just wrote in case anybody had a good
> idea.
That one's not so easy; there's no standard solution for this problem.
I recommend that you follow Aahz' suggestion of asking on c.l.py
instead.
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev
mailing list