self modifying code

Robin Becker robin at NOSPAMreportlab.com
Sat Apr 29 15:01:14 EDT 2006


John J. Lee wrote:
> 1. I don't think most people would call that "self-modifying code".  I
>    won't try defining that term precisely because I know you'll just
>    pick holes in my definition  ;-) 


Don't really disagree about the rewriting code, but the function does 
re-define itself.


> 2. The use of global func is just plain weird :-)
> 
> 3. Peter Otten's version is OK, but how about this, using a closure
>    instead of globals (UNTESTED)
> 
> def make_func():
>     namespace = object()
>     namespace.data = None
>     def func(a):
>         if namespace.data is None:
>             namespace.data = somethingcomplexandcostly()
>         return simple(namespace.data, a)
>     return func
> func = make_func()
> 
.......
the inner function is almost precisely what I started with, except I 
used the global namespace. However, it keeps the test in side the 
function which costs about 1%.
-- 
Robin Becker



More information about the Python-list mailing list