Using id to delete duplicates
We reviewed the id
function in the previous chapter, which covered introspection. In Python 3, id
is used to identify an object using the object’s memory address. Identifying id
of an object can be used to reflect on an object and avoid redundancies or errors that can possibly occur while using an object throughout a program.
To understand this further, we will be creating a class named Coupon
, which generates a unique random coupon ID and prints a coupon for any product that is provided as input. In the following code, we will start with creating a class named Coupon
and will be adding the coupon details as attributes. We will also create a method named generate_coupon
to print five coupons for a product along with its unique random coupon ID:
class Coupon:
def __init__(self, product_name, product_category, \
brand,source, expiry_date, quantity):
...