[Tutor] The Best Way to go About with Self Modifying Code/Code Generation?

Alan Gauld alan.gauld at btinternet.com
Sun Jul 8 01:21:56 CEST 2012


On 07/07/12 22:19, Aaron Tp wrote:

> Hey all, I have a question on using self-modifying code/code generation in Python;
> namely how to do it.

Self generating code is hard to do.
Writing code is hard for intelligent humans, writing code to generate 
code is even harder.

To write code that modifies itself increases the difficulty even further 
(and debugging it by an order of magnitude because you no longer know 
what the code you are debugging is!)

If you are going to do it, its best if you use a language where the code 
and data are interchangeable, and that mainly means some kind of Lisp 
type language. To use a traditional language like Python just makes it 
even more complex.

So in short you have bitten off a huge challenge.

But there are ways and means to do some basic things depending on what 
you need to do. If its just changing the sequencing or control flow of a 
pre-existing set of functions then that's not too bad. Or if its just 
varying some parameters in an existing algorithm again it is doable.
If its dynamically creating new functions or completely changing an 
existing algorithm then you are into really murky waters!

> then using 'exec' to execute the statements...

exec or more likely eval()  is pretty essential if you truly are writing 
new functions/algorithms. But its not needed if just changing control 
sequences.

> The first problem here, of course, is that it isn't a self-modifying
> system at all....second being there'd be no way to edit pre-existing
 > code, and getting it all to be deleted wouldn't...make any sense.

That depends on what you are trying to do.
But one thing that will help a lot is to express your program ass a 
state machine. Then express the state machine as a data table with each 
state represented as an index into the table and with functions to 
transition between states. Then write the functions and put them in a 
dispatch table accessed via indexes.

Now your programs behaviour can be expressed as a series of integers(the 
function indexes) and a state machine expressed as a table. Now we have 
code as data, albeit a limited set of code.
And you can modify the behaviour by modifying the index corresponding to 
a state change and by modifying the next state index.

>
> People have recommended the use of Python's AST module, along with
 > the Compile module,

Thats probably the best bet if you really need to generate/.modify 
existing functions.

> ...Or am I jst being overly-ambitious?

You don't tell us anything about your Python coding experience (or
even you're coding experience in other languages) but it is a big 
challenge. Especially in a language like Python (although much
easier than in a compiled language like C!! And even that is possible: I 
did once implement a state machine solution as above in C  for a remote 
controlled network traffic-routing switch)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/





More information about the Tutor mailing list