Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Metaprogramming with Python

You're reading from   Metaprogramming with Python A programmer's guide to writing reusable code to build smarter applications

Arrow left icon
Product type Paperback
Published in Sep 2022
Publisher Packt
ISBN-13 9781838554651
Length 402 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Sulekha Aloorravi Sulekha Aloorravi
Author Profile Icon Sulekha Aloorravi
Sulekha Aloorravi
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Preface 1. Part 1: Fundamentals – Introduction to Object-Oriented Python and Metaprogramming
2. Chapter 1: The Need for and Applications of Metaprogramming FREE CHAPTER 3. Chapter 2: Refresher of OOP Concepts in Python 4. Part 2: Deep Dive – Building Blocks of Metaprogramming I
5. Chapter 3: Understanding Decorators and their Applications 6. Chapter 4: Working with Metaclasses 7. Chapter 5: Understanding Introspection 8. Chapter 6: Implementing Reflection on Python Objects 9. Chapter 7: Understanding Generics and Typing 10. Chapter 8: Defining Templates for Algorithms 11. Part 3: Deep Dive – Building Blocks of Metaprogramming II
12. Chapter 9: Understanding Code through Abstract Syntax Tree 13. Chapter 10: Understanding Method Resolution Order of Inheritance 14. Chapter 11: Creating Dynamic Objects 15. Chapter 12: Applying GOF Design Patterns – Part 1 16. Chapter 13: Applying GOF Design Patterns – Part 2 17. Chapter 14: Generating Code from AST 18. Chapter 15: Implementing a Case Study 19. Chapter 16: Following Best Practices 20. Other Books You May Enjoy

Using callable to dynamically check and generate methods

We will now look into another familiar function named callable to check on how it can be used to perform reflection on an object.

Note

A class, method, or a function is callable in Python, while class objects or variables are not callable.

In this example, we will check whether a class is callable, and if it returns true, we will dynamically add a method to the class. To test the usage of the callable function, we will first create a coupon class and name it SimpleCoupon:

class SimpleCoupon:
    pass

Let’s check whether the preceding class is callable:

callable(SimpleCoupon)
True

In the following code, we will create a function that generates another function if called. We will create a create_coupon function that, in turn, creates a generate_coupon function or method when called:

def create_coupon( product, product_category, brand, source, expiry_date, quantity:
...
lock icon The rest of the chapter is locked
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Metaprogramming with Python
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Modal Close icon
Modal Close icon