Checking whether an object has an attribute
While using a method or a function object defined in a framework or library by importing the library into another program, we might not always know all the attributes of the object. In such cases, we have a built-in hasattr
function that can be used to introspect if a Python object has a specific attribute.
This function checks whether a given object has attributes. To test this function, we will create a class for the inventory of ABC Megamart, add the required attributes for the products stored in the inventory, and also include the price of the products along with the tax component. The price will be calculated both before and after tax for the products stored in the inventory. The following are the steps for it:
- We will create a class called
Inventory
and initiate it with the variables required for an inventory, such asproduct_id
,product_name
,date
(of purchase),unit_price
,quantity
,unit_discount
, andtax
, as shown in the...