I am creating a simple AI program in Python 2.7 and i was going to make it be able to learn. Is there any way to have it so the script could edit itself, like adding a answer to a question into it's own code, in a certain spot in the code.
thank-you in advance guys!
When a Python interpreter is invoked on a script, it is parses and transforms it into byte-code.. this leaves a .pyc file which is actually what executed.
A script could write into itself but that would not cause parsing to restart.
You could open the file containing the code using:
module = __import__(__name__)
with open(module.__file__) as f:
print f.read()
Although it is not recommended to dynamically alter the source code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With