Python Programming – Built-in Class Attributes
We have seen in the previous sections that how to create a class and the attributes of a class. As we know that class is a user-defined data type. The programmer can create his own data type in the form of a class, which follows the concept of object-oriented programing structures. While creating a class, we specify our own attributes. However, certain attributes intrinsically associated with the class, when it is created. The list of built-in attributes is given in Table 10.2. with the description of each. The programming example for the same is given in Code 10.9. As we can see from the program that the first built-in attribute diet gives the information about the class variables itemCount and total_bill and class functions display them(), init () along with other details.
The doc attribute gives the information about the document string which is “An inventory class” for this code. Subsequently, the named module provides the class name which is ‘inventory’. the module gives the main module. In the end, the bases attribute gives the “(<class ‘object’>,)” since there is no base for this class. It is to be noted that the class built-in attributes are accessed with the class name, dot (.) operator, and attribute not with the object of the class as in the previous code given in 10.8.
- Python Programming – Editing Class Attributes
- Python Programming – Classes and Objects
- Python Programming – Built-in Functions
Attribute | Description |
__diet__ | Dictionary containing the class’s namespace |
__ doc__ | Class documentation string or none, if undefined |
__ name__ | Class name |
__ module__ | Module name in which the class is defined. This attribute is ” main ” in interactive mode. |
__ bases__ | A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list. |
Code’: 10.9. Illustration of using built-in class attributes
#Illustration of using built-in class attributes class Inventory: def__init__(self, item, price, quantity):#initialization or def displayltem(self): #displayStudent method of 11 =Inventory(‘Soap’, 30.5, 4) |
Output {‘ weakref <attribute ‘ weakref_’ of ‘Inventory’ objects>, ‘_dict <attribute ‘ diet ‘ of ‘Inventory’ objects>, ‘ init ‘: <function Inventory. __init__ at 0x02D03390>,’ doc ‘: ‘An inventory class!, ‘total bill’: 122.0, ‘displayltem’: <function Inventory.displayltem at 0x02D034B0>,’ module ‘: ‘ main ‘, ‘itemCount’: 1} An inventory class Inventory __main__ (<class ‘object’>,) |