Abstract
The `ast` module allows you to manipulate the AST (Abstract Syntax Tree) and change the behavior of the code at runtime.
I would like to introduce it with an interesting use case on Pyodide, a Python distribution for the browser. Pyodide has some differences from the CPython due to its asynchronous nature, so the code that works on CPython may not work on Pyodide. For example, `time.sleep()` does not work on Pyodide and you need to use `await asyncio.sleep()` instead.
However, you may want to run the code written for CPython on Pyodide for interoperability.
In such a case, you can modify the AST to make it work on Pyodide without rewriting the source code.
In this talk, I will explain the AST manipulation with the `ast` module.
Then I will share some practical examples that I actually implemented in my Pyodide-based project to run CPython-targeted code.
They should have insights about how to use the `ast` module to solve some problems with actual examples.