Metaprogramming is not often something that we need to use, but when we do it is there. If avoidable, it is probably recommended to not rely on metaprogramming because it is hard for other programmers to understand. In this hands-on lab, we’ll be building a metaclass to make it easy to create database models. By the time you’ve finished this lab, you’ll have demonstrated a good grasp of how to create a metaclass and a common way to use metaprogramming.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Create ModelClass Metaclass and Add a Dynamic Initializer
Create a subclass of the
type
class and implement the__new__
method. From here, create and attach an initializer based on the annotations on the class.- Build and Attach get_by_xxx Class Methods for Each Attribute on the Model
For each of the annotations on the class build a
get_by_xxx
method and attack it as a class method.