Advanced Python: Metaprogramming
Published by Pearson
Learn techniques for better Python debugging, readability and library design
- Intended for advanced Python users who want to really dig into how Python works.
- Covers techniques used commonly in large, popular, mature libraries such as Django, Starlette, FastAPI, Pydantic, or Textual, as well as in the Python standard library in modules such as dataclasses, functools, collections, or enum.
- Discusses the risks and rewards of using these sophisticated techniques, in terms of readability, correctness, and maintainability.
- Deep dives into class construction, library design, and metaprogramming techniques including better library design and debugging.
To implement complex systems, frameworks, or libraries, or to debug tricky issues in said systems, you need a thorough understanding of how Python really works under the hood. This course tackles topics not typically covered in Python classes, such as class construction, what a metaclass is at its core, library design, metaprogramming and monkey-patching, and practical ways to tackle complex problems using these strategies.
Metaprogramming is code that writes code. In Python, this is accomplished through a variety of techniques, such as the use of metaclasses, class decorators, object model methods like init_subclass, and mechanisms such as eval and exec. These techniques are in heavy use in popular, large, mature libraries like Django or Starlette, FastAPI, Pydantic, or Textual. If you need to debug code that uses any of these frameworks, then you may need to understand the common metaprogramming techniques. Additionally, if you want to write large, complex libraries, metaprogramming approaches allow you to eliminate crude, error-prone manual techniques and provide opportunities for creating interfaces that users find easy and intuitive.
What you’ll learn and how you can apply it
- Explain how the Python execution model really works, including what happens defining a class.
- Identify the kinds of problems that metaclasses solve.
- Know when to use advanced techniques like monkey-patching and eval and exec.
This live event is for you because...
- You are a strong, advanced Python user
- You want to develop a thorough understanding of how Python works to implement complex systems, frameworks, or libraries
- You want to debug tricky issues in complex systems
- You want to learn new techniques to dig into how Python works under the hood
Prerequisites
- Strong Python experience (≥50k lines of code)
Course Set-up
- No specific set-up required. Course notes and all materials provided during the session.
- Recommended: Up-to-date Python installation (from www.python.org) and coding environment.
Recommended Preparation
- Read: Python Distilled by David Beazley
Recommended Follow-up
- Read: Python Cookbook, 3rd Edition, by David Beazley and Brian K. Jones
- Attend: Advanced Python: API Design with Object Orientation by James Powell
- Attend: Advanced Python: Functional Programming by James Powell
Schedule
The time frames are only estimates and may vary according to how the class is progressing.
Segment 1: Execution Model (50 mins)
- How does the Python execution model actually work?
- What happens when I
importa module in Python? - What happens when I define a
class? - Q&A
Break (10 mins)
Segment 2: class Construction (50 mins)
- What is
__init__vs__new__? - What is a
metaclass, at its core? - What is
__prepare__or__call__? What is__get__and__set_name__? - Q&A
Break (10 mins)
Segment 3: Library Design (50 mins)
- What problems do
metaclasses actually solve? - What is
__build_class__and__init_subclass__and how may they be applied to solve similar problems? - What is a
classdecorator, and how does it compare to these other approaches? - Q&A
Break (10 mins)
Segment 4: Metaprogramming (50 mins)
- What is
evalandexec? - What is monkey-patching?
- When should I actually use these advanced techniques?
- What are some extremely useful examples of applying these techniques to debug complex systems?
- Q&A
**Course wrap-up and next steps (10 minutes)**
Your Instructor
James Powell
James Powell is a world-renowned expert in data science and open source scientific computing. His subject mastery and guidance are sought after by global organizations for consulting, coaching, and staff training. He is one of the most prolific speakers in the community, and he shares new content each week on his YouTube page, LinkedIn, and Discord.
In addition to O’Reilly Online Learning, James and his team at Don't Use This Code provide Python training and consulting services. He would love to talk to you about how your team uses open source tools for AI/ML - e-mail him at james@dutc.io.
James’s courses are dense and fast-paced. Be sure to stay for the full session and don’t worry if it goes by quickly. You’ll want to re-watch the video later to capture all of the details.