Understanding abstract syntax trees with applications
In this section, we will look into applying the concept of abstract syntax trees to our core example of ABC Megamart and explore how ast
is defined in the classes, such as the Branch
class and the VegCounter
class of ABC Megamart. We will also look at modifying the behavior of these classes using ast
at a metaprogramming level instead of modifying the actual source code of the class.
Understanding the ast of a class
In this section, we will look at understanding the abstract syntax tree of a class, which will help us in exploring how to modify the elements of a class through metaprogramming. We can try it as follows:
- Let’s begin by creating a class with empty definitions and look at its abstract syntax tree:
branch_code = """ class Branch: '''attributes...''' '''methods...''' ""...