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...