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

Introducing built-in functions used in reflection

To understand reflection and the usage of Python’s built-in functions to perform reflection, we will continue making use of our core example of ABC Megamart throughout this chapter. We will be specifically looking at the concept and examples based on coupons in a retail store throughout this chapter. Coupons are a technique used by retail stores or manufacturers to promote their products among consumers. Coupons are generated and posted through various modes of advertisements, and they are used to attract customers to a specific store or product.

We will make use of the same set of built-in functions as in introspection, to apply reflection on Python objects:

  • id()
  • eval()
  • callable()
  • hastattr()
  • getattr()
  • isinstance()
  • issubclass()
  • property()

Reflection on Python objects helps in the metaprogramming of these objects using the built-in functions that can introspect Python objects, and...

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 €18.99/month. Cancel anytime
Modal Close icon
Modal Close icon