Using hasattr to set values
We will now look into the hasattr
function, which can be used to check whether a Python object has attributes. Using this function as a condition to test the objects, we can apply reflection on objects externally.
In this example, we will look at creating custom coupons by changing one of its variables using the hasattr
function. The classes and methods throughout this chapter are used to understand reflection with relevant examples explained under each section. We will now create another class named CustomCoupon
. We will add and define class attributes to this class within the class itself and we will be adding a method to generate coupons:
class CustomCoupon:
product_name = "Honey Mustard Sauce"
product_category = "Condiments"
brand = "ABCBrand3"
source = "Store"
expiry_date...