Self modifying systems question
Duncan Booth
duncan at NOSPAMrcp.co.uk
Wed Apr 30 08:37:04 EDT 2003
tebeka at cs.bgu.ac.il (Miki Tebeka) wrote in
news:33803989.0304300426.402cd1ef at posting.google.com:
>> Self modifying code is a well known technology. Has anyone done this in
>> Python? What I'm seeking for is examples of software that adds to
>> itself (methods and variables) in runtime. I'd like an instantiated
>> object to
>> be able to add a method to itself.
>>>> class A:
> pass
>
>>>> a = A()
>>>> a.f = lambda x: x + 1
>>>> a.f(100)
> 101
That doesn't really add a method to 'a', it adds a function, but the
function doesn't have any way to determine which instance it was called on.
You need to simulate the same binding that you get when calling a method
defined in the class.
Here is one way to add a method to an instance (Python 2.2 and on):
>>> def f(self):
return self.x + 1
>>> class A: pass
>>> a.x = 5
>>> a.f = f.__get__(a)
>>> a.f()
6
>>>
--
Duncan Booth duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
More information about the Python-list
mailing list