Re: self modifying code
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: self modifying code

From: Robin Becker <robin@NOSPAMreportlab.com>
Date: Sat Apr 29 2006 - 20:04:23 CEST

Peter Otten wrote:
....
>>
>> def func(a):
>> global func, data
>> data = somethingcomplexandcostly()
>> def func(a):
>> return simple(data,a)
>> return func(a)
>>
........
>
> at the cost of just one object identity test whereas your func()
> implementation will do the heavy-lifting every time func() is called in the
> client (unless func() has by chance been invoked as lazymodule.func()
> before the import).

in the example code the heavy lifting, costly(), is done only once as
the function that does it is overwritten. As pointed out it won't work
as simply in a class. Memoisation could improve the performance of the
normal case form of the function ie

def func(a):
     return simple(data,a)

but I guess that would depend on whether simple(data,a) is relatively
expensive compared to the costs the memo lookup.

-- 
Robin Becker
Received on Mon May 1 00:44:30 2006