Metaprogramming with Python: A programmer’s guide to writing reusable code to build smarter applications
- Length: 402 pages
- Edition: 1
- Language: English
- Publisher: Packt Publishing
- Publication Date: 2022-09-09
- ISBN-10: 1838554653
- ISBN-13: 9781838554651
- Sales Rank: #1431677 (See Top 100 Books)
A practical approach to metaprogramming with real-world examples that enables the development of advanced frameworks, libraries, and applications using Python
Key Features
- Learn applied metaprogramming through a simple step-by-step approach
- Work with easily understandable examples and explanations that take you deep into the theory of metaprogramming
- Get practical experience in writing reusable code with real-world examples
Book Description
Effective and reusable code makes your application development process seamless and easily maintainable. With Python, you will have access to advanced metaprogramming features that you can use to build high-performing applications.
The book starts by introducing you to the need and applications of metaprogramming, before navigating the fundamentals of object-oriented programming. Next, you will learn about simple decorators, work with metaclasses, and later focus on introspection and reflection. You’ll also delve into generics and typing before defining templates for algorithms. As you progress, you will understand your code using abstract syntax trees and explore method resolution order. This Python book also shows you how to create your own dynamic objects before structuring the objects through design patterns. Finally, you will learn simple code-generation techniques along with discovering best practices and eventually building your own applications.
By the end of this learning journey, you’ll have acquired the skills and confidence you need to design and build reusable high-performing applications that can solve real-world problems.
What you will learn
- Understand the programming paradigm of metaprogramming and its need
- Revisit the fundamentals of object-oriented programming
- Define decorators and work with metaclasses
- Employ introspection and reflection on your code
- Apply generics, typing, and templates to enhance your code
- Get to grips with the structure of your code through abstract syntax trees and the behavior through method resolution order
- Create dynamic objects and generate dynamic code
- Understand various design patterns and best practices
Who this book is for
If you are an intermediate-level Python programmer looking to enhance your coding skills by developing reusable and advanced frameworks, then this book is for you. Basic knowledge of Python programming will help you get the most out of this learning journey.
Metaprogramming with Python Contributors About the author About the reviewers Preface Who this book is for What this book covers To get the most out of this book Download the example code files Download the color images Conventions used Get in touch Share Your Thoughts Part 1: Fundamentals – Introduction to Object-Oriented Python and Metaprogramming Chapter 1: The Need for and Applications of Metaprogramming Technical requirements An overview of metaprogramming Metaprogramming – a practical introduction Metadata of the add function Resolving type errors using metaprogramming Understanding why we need metaprogramming Don’t Repeat Yourself Exploring the applications of metaprogramming Summary Chapter 2: Refresher of OOP Concepts in Python Technical requirements Introducing our core example Creating classes Understanding objects Applying methods Implementing inheritance Extending to multiple inheritance Understanding polymorphism Polymorphism within inheritance Polymorphism in independent classes Hiding details with abstraction Protecting information with encapsulation Private members Protected members Summary Part 2: Deep Dive – Building Blocks of Metaprogramming I Chapter 3: Understanding Decorators and their Applications Technical requirements Looking into simple function decorators Understanding function decorators with an application Exchanging decorators from one function to another Applying multiple decorators to one function Exploring class decorators Understanding class decorators with an application Getting to know built-in decorators The static method The class method Summary Chapter 4: Working with Metaclasses Technical requirements Overview of metaclasses The structure of a metaclass Analyzing the arguments The application of metaclasses Inheriting the metaclass Inheriting as a parent and metaclass Switching metaclasses Inheritance in metaclasses Manipulating class variables Summary Chapter 5: Understanding Introspection Technical requirements Introducing built-in functions Using the built-in id function Debugging unintentional assignments using id Finding out whether an object is callable Checking whether an object has an attribute Checking whether an object is an instance Checking whether an object is a subclass Understanding the usage of property Using property as a decorator Summary Chapter 6: Implementing Reflection on Python Objects Technical requirements Introducing built-in functions used in reflection Using id to delete duplicates Using callable to dynamically check and generate methods Using hasattr to set values Using isinstance to modify an object Using issubclass to modify a class Applying property on a class Summary Chapter 7: Understanding Generics and Typing Technical requirements What are generics? How are generics connected to metaprogramming? How are generics handled in Python? What happens when data types are specified? Type hints as annotations Typing with explicit type checks – approach 1 Creating a class to implement type checking Creating a class to test type checking Typing with explicit type checks – approach 2 Creating a class to implement type checking Creating a class to test type checking Adding data types with constraints Creating a simple custom data type Creating a domain-specific data type Summary Chapter 8: Defining Templates for Algorithms Technical requirements Explaining a sequence of operations Back to our core example The vegetables and dairy counter Less than 10 items counter The greater than 10 items counter Electronics counter Defining the sequence of methods The vegetable counter Less than 10 items counter Greater than 10 items counter The electronics counter Identifying the common functionalities Designing templates Summary Part 3: Deep Dive – Building Blocks of Metaprogramming II Chapter 9: Understanding Code through Abstract Syntax Tree Technical requirements Exploring the ast library Inspecting Python code with abstract syntax trees Reviewing simple code using ast Modifying simple code using ast Understanding abstract syntax trees with applications Understanding the ast of a class Modifying the ast of a code block by parsing Modifying the ast of a code block by transforming nodes Summary Chapter 10: Understanding Method Resolution Order of Inheritance Technical requirements Understanding the MRO of a class Understanding MRO in single inheritance Understanding MRO in multiple inheritances Reviewing MRO in multilevel inheritance Understanding the importance of modifying the order of inheritance Impact of unintended change of order in inheritance Summary Chapter 11: Creating Dynamic Objects Technical requirements Exploring type for dynamic objects Creating multiple instances of a class dynamically Creating dynamic classes Creating multiple dynamic classes Creating dynamic attributes and methods Defining attributes dynamically Defining methods dynamically Summary Chapter 12: Applying GOF Design Patterns – Part 1 Technical requirements An overview of design patterns Exploring behavioral design patterns Understanding the chain of responsibility Learning about the command design pattern The strategy design pattern Summary Chapter 13: Applying GOF Design Patterns – Part 2 Technical requirements Exploring structural design patterns Understanding the bridge pattern Understanding the facade pattern Understanding the proxy pattern Exploring creational design patterns Understanding the factory method Understanding the prototype method Understanding the singleton pattern Summary Chapter 14: Generating Code from AST Technical requirements Generating a simple class with a template Generating multiple classes from a list Generating a class with attributes Generating a class with methods Generating a class with an init method Generating a class with a user-defined method Defining a custom class factory Developing a code generator to generate a simple library Summary Chapter 15: Implementing a Case Study Technical requirements Explaining the case study Defining base classes Developing a code generator library Generating code Designing an execution framework Summary Chapter 16: Following Best Practices Technical requirements Following PEP 8 standards Indentation Neat representation Writing clear comments for debugging and reusability Adding documentation strings Documentation string for metaprogramming Naming conventions Class names Variables Functions and methods Avoiding the reuse of names Avoiding metaprogramming where not required Summary Why subscribe? Other Books You May Enjoy Packt is searching for authors like you Share Your Thoughts
Donate to keep this site alive
How to download source code?
1. Go to: https://github.com/PacktPublishing
2. In the Find a repository… box, search the book title: Metaprogramming with Python: A programmer’s guide to writing reusable code to build smarter applications
, sometime you may not get the results, please search the main title.
3. Click the book title in the search results.
3. Click Code to download.
1. Disable the AdBlock plugin. Otherwise, you may not get any links.
2. Solve the CAPTCHA.
3. Click download link.
4. Lead to download server to download.