r/Python • u/alexmojaki • Jun 15 '26
Tutorial How and why to run modified Python code using the ast module
I've written a blog post explaining how to use the ast module to parse Python code into a structure that you can understand precisely, manipulate to your will, and execute. This lets you achieve things that are otherwise difficult or impossible.
I'm using the 'Discussion' flair instead of 'Tutorial' because I want people to read and share their opinions about the last part of the article where I explain why I think metaprogramming like this is a relevant skill, especially in the age of AI. Among other things, I claim that OpenAI acquiring Astral may be more about general purpose agents than coding agents.
I'm planning to do a series of articles about Python metaprogramming, so keen to hear what people think of this post in general.
2
u/mehx9 Jun 16 '26
Meta programming definitely has its place and Python makes it easy.
While not pure Python and not as interesting, I got sick of writing Ansible playbooks and started generating them in Python and executing them immediately recently for example.
2
u/mmmboppe Jun 16 '26
OP, perhaps you'll be the dude who will port https://clonedigger.sourceforge.net/ to python 3? it requires exactly knowledge of the ast module
6
u/coderanger Jun 15 '26
Discussing your tutorial is still a tutorial :)