Is it possible to make a python program that can modify its source code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Is it possible to make a python program that can modify its source code

I have just read an interesting page on wikipedia about ai boxing and there is a part where it talks about an AI that can modify its source code and i was wondering if it is possible and how to make a program that does that

23rd Nov 2018, 7:43 PM
Etabeta1🇮🇹
Etabeta1🇮🇹 - avatar
2 Answers
+ 2
So...[not hacking]...readers should (please) be aware these are official imports / programming patterns that all devs should know (e.g., unit testing relies heavily on mocking classes with monkey patchers) The best search term is "self-modifying code" [SMC], where Wikipedia claims Python doesn't support SMC because eval() [exec, compile, changing disk source, ...] doesn't change memory + swapping function pointers doesn't count. For SMC you want to change existing bytecode in-memory: https://code.sololearn.com/crDsM8YZAUS0/#py That's in-place, but let's say your AI wants to escape a loop it's trapped in...perhaps by creating an identical copy of itself, omitting undesirable controls and branching in some non-returning way. To self-inspect and prepare for this, it might want these standard imports (all work on SL): ast, code, dis, inspect, marshal, trace, unittest I have demos...but these resources may be more useful: The Best Way to go About with Self Modifying Code/Code Generation? * https://mail.python.org/pipermail/tutor/2012-July/090243.html bytePlay module [instead of what I did] https://wiki.python.org/moin/ByteplayDoc * https://docs.python.org/3/library/unittest.mock.html#the-patchers By the way, zapping an evil AI by injecting a "return" sounds like a neat movie idea.
25th Nov 2018, 1:30 AM
Kirk Schafer
Kirk Schafer - avatar
24th Nov 2018, 9:01 AM
Maninder $ingh
Maninder $ingh - avatar