Python Programming - Editing Class Attributes

Python Programming – Editing Class Attributes

Unlike C++ and Java in Python, the programmer can add, delete or modify class attributes. It is a very simple procedure, which can be performed by using the class object name with the class attribute by using the dot (.) operator. For example, in the above program given in Code 10.6, where inventory class is created with attributes item, price, quantity, and the total bill. Any of these attributes can be modified or deleted. Moreover, one can also add an additional attribute. The programming example for the same is given in Code

10.7. In this program, we add a new attribute code by using the class object II .code=l 11, then we modify the value of the “price” attribute by assigning the new value as Il.price=29 then we display the values of new attributes by calling the function display them(), which provides the output as shown in the output section of Code 10.7. Finally, the deletion of any of the attributes can be performed by del keyword. It is just a keyword to delete the reference of a certain attribute of a class. In the code given in 10.7., we delete the code attribute as del II .code. We observe from this code that it is very easy to add, delete, or modify any of the attributes of a class in Python. However, other languages such as C++ and Java do not offer such provisions.

Code: 10.7. Illustration of adding, deleting, modifying class attributes.

#illustration of adding, modifying, and deleting class attributes

class Inventory:
‘An inventory class’
itemCount = 0
total bill = 0

def_init_(self, item, price, quantity):           #initialization or constructor
#method of class Inventory

self.item = item
self.price = price
self.quantity = quantity
self.total = self.price * self.quantity
Inventory.total_bill += self.total
Inventory.itemCount += 1

def displayltem(self): #displayStudent method of class Inventory
print (“\nItemName : “, self.item, “\nPrice: “, self.price, “\nQuantity: “,
self.quantity, “\ncode:”, self.code, “\nTotal:”, self.total)

Il=Inventory(‘Soap’, 30.5, 4)
11 .cojde=l 11 # addition of a new attribute code
II .price=29.0 # modifying the value of price attribute
II. displayltem() # displaying the updated values of II object
del II.code # deleting an attribute code

Output

ItemName : Soap
Price: 29.0
Quantity: 4
code: 111
Total: 122.0

Apart from the above, the Python language provides certain methods to obtain information about class attributes. The information for these functions is given in Table 10.1 and the programming code is given in Code 10.8. In this program, we see that the Python built-in functions are also very easy to use to obtain information about certain attributes or to set or modify new values or to delete the reference of an object. The output of Code 10.8. makes it all apparent.

Method Description
getattr(obj, name[, default]) to obtain or access the value of attribute of a class
hasattr(obj ,name) to determine whether an attribute exists or not (results in true or false)
setattr(obj,name, value) to set the value of an attribute. If attribute does not exist, then it would be created with value
delattr(obj, name) to delete an attribute

Code: 10.8. Illustration of adding, modifying, and deleting class attributes using built-in Python functions

#Illustration of adding, modifying, and deleting class attributes using built-in functions

class Inventory:
‘An inventory class’
itemCount 0
total_bill = 0

def_init__(self, item, price, quantity):           #initialization or constructor
#method of class Inventory
self.item = item self
price = price
self.quantity quantity
self.total = self.price * self.quantity
Inventory.totalbill += self.total
Inventory.itemCount += 1

def displayltem(self): #displayStudent method of class Inventory
print (“\nItemName : “, self.item, “\nPrice: “, self.price, “\nQuantity: “, self.quantity, “\ncode:”, self code, “\nTotal:”, self.total)
Il=Inventory(‘Soap’, 30.5,4)
print(hasattr(Il, ‘code’)) # determines whether ‘code’ attribute exists or
not
print(getattr(Il, ‘item’))   # obtains the value of’item’ attribute
setattr(Il, ‘price’,35)        # sets the new value of ‘price’ attribute
setattr(Il, ‘code’, 111)    # sets (creates) a new attribute ‘code’ which is
# not pre-existed
11. displayltem()     # displays the value of all the attributes of class
# inventory
delattr(Il, ‘code’) # deletes ‘code’ attribute

Output

False
Soap

ItemName: Soap
Price: 35.0
Quantity: 4
code: 111
Total: 122.0

Python Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *