Re: self modifying code
- From: Robin Becker <robin@xxxxxxxxxxxxxxxxxxx>
- Date: Sat, 29 Apr 2006 18:04:23 +0000
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
.
- References:
- self modifying code
- From: Robin Becker
- Re: self modifying code
- From: Peter Otten
- self modifying code
- Prev by Date: Re: How to get computer name
- Next by Date: opposite function to split?
- Previous by thread: Re: self modifying code
- Next by thread: Re: self modifying code
- Index(es):
Relevant Pages
|