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.
- Python Programming – Classes and Objects
- Python Programming – Operations on Files
- Python Programming – Built-in Functions
Code: 10.7. Illustration of adding, deleting, modifying class attributes.
#illustration of adding, modifying, and deleting class attributes class Inventory: def_init_(self, item, price, quantity): #initialization or constructor self.item = item def displayltem(self): #displayStudent method of class Inventory Il=Inventory(‘Soap’, 30.5, 4) |
Output ItemName : Soap |
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: def_init__(self, item, price, quantity): #initialization or constructor def displayltem(self): #displayStudent method of class Inventory |
Output False ItemName: Soap |