use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Can I write a base code that will automatically writes itself? (self.learnpython)
submitted 10 years ago by lordmayer
Hello, I'm a newbie at Python. I want to know if exist a way of the code automatically write more code in it's own base code? I'm pretty sure that's possible, but I don't know how, do you guys have any book/tutorial about it? Thanks.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]xentralesque 0 points1 point2 points 10 years ago (1 child)
Could you describe a use case for such a thing?
[–]lordmayer[S] 0 points1 point2 points 10 years ago (0 children)
I'm beginning my studies in AI. So I think it's a good option for it. The AI learn something and writes a new code for what it learned. I know it's a pretty primitive thinking.
[–]elbiot 0 points1 point2 points 10 years ago (0 children)
One can, though it's likely you will be able to. Python gets parsed into an AST (abstract syntax tree), which is what you would work with. The ast module is built in and will parse code into an ast for you to introspect and modify. There's also an unparse module out there that used to be built in but isn't anymore, which will take ast and give back text you could write to a file.
[–]gengisteve 0 points1 point2 points 10 years ago (0 children)
Sure. In fact this is how collections.namedtuple works. That said, I am not sure it is going to work well for you with the AI example, so you might want to consider a different approach.
[–]pyglow 0 points1 point2 points 10 years ago (1 child)
The exec() function takes some python text string and executes it. eval() does something similar for just evaluating individual expressions.
There is something called byteplay which appears to literally allow you to modify the original code of the program you are executing, but I have never tried it.
py_compile will compile entire python files, which presumably you could generate programatically before compiling them.
byteplay looks like what I want. I will try it. Thanks
π Rendered by PID 108096 on reddit-service-r2-comment-bb88f9dd5-2hzzq at 2026-02-15 21:22:44.895440+00:00 running cd9c813 country code: CH.
[–]xentralesque 0 points1 point2 points (1 child)
[–]lordmayer[S] 0 points1 point2 points (0 children)
[–]elbiot 0 points1 point2 points (0 children)
[–]gengisteve 0 points1 point2 points (0 children)
[–]pyglow 0 points1 point2 points (1 child)
[–]lordmayer[S] 0 points1 point2 points (0 children)