self modifying code



When young I was warned repeatedly by more knowledgeable folk that self modifying code was dangerous.

Is the following idiom dangerous or unpythonic?

def func(a):
global func, data
data = somethingcomplexandcostly()
def func(a):
return simple(data,a)
return func(a)

It could be replaced by

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

but this always calculates data.
--
Robin Becker
.



Relevant Pages

  • Re: self modifying code
    ... modifying code was dangerous. ... def func: ... won't try defining that term precisely because I know you'll just ... The use of global func is just plain weird :-) ...
    (comp.lang.python)
  • Re: self modifying code
    ... def func: ... The use of global func is just plain weird :-) ... this doesn't work because you can add attributes to plain object instances: ...
    (comp.lang.python)
  • Re: Passing function objects to timeit
    ... def test_timer(func1, func2): ... global gFUNC ...
    (comp.lang.python)
  • Re: self modifying code
    ... modifying code was dangerous. ... def func: ... global func, data ... from lazymodule import func ...
    (comp.lang.python)