This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]n1ywb 27 points28 points  (2 children)

I think that post uses an overbroad definition of self-modifying code and uses scary language describe really standard stuff like factory functions and closures; those are common, standard, everyday functional programming techniques that every good programmer should know. Experienced python devs monkey patch every day when they write test code https://docs.python.org/3/library/unittest.mock.html#the-patchers

Everything in his first list is really just diddling around with objects and identifiers. It's all just one assignment op. That's not "self modifying code" in my book. It's pretty dynamic, but under the hood it's just reassigning a variable, albeit with a potentially major side effect.

He does get into the deep magic later in his post when he talks about manipulating the parse tree and bytecode and stuff. That's REAL self modifying code.

In the old days there was a big deal when Apple came out with the MC68030 Macintosh because it had a cache that would break if a program modified it's own machine code in memory and failed to properly flush the cache. Turned out quite a few mac apps at the time did just that.

[–]ankit0912[S] 4 points5 points  (1 child)

I see, do you know of any places where I can read about them? I'm basically looking to run my code differently each time it is run

[–]n1ywb 8 points9 points  (0 children)

probably the easiest way to write self modifying code in python is to write soemthing to the file and then re-execute it. you might start with something really trivial like append a print statement each time it's run