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 isinstance to modify an object

We will now look at another built-in function named isinstance. This function is used to identify whether an object is an instance of a class. We will be implementing reflection on class objects by checking whether they are instances of a specific class and then customizing the object of the class accordingly. This example uses the same attributes (source, store, and manufacturer) as in the preceding example of the hasattr function.

To begin with, let’s create two objects for two different classes and apply the isinstance function to the objects of the classes to understand how this function can help in changing the behavior of Python objects. We will be reusing the CustomCoupon class from the preceding section and we will also be creating another SimpleCoupon class. We will then add two objects, coupon1 and coupon2, as follows:

coupon1 = CustomCoupon()
class SimpleCoupon:
    product_name = "Strawberry...
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