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.

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:
‘An inventory class’ .
itemCount = 0
total_bill = 0

def__init__(self, item, price, quantity):#initialization or
#method of class
self.item = item
self.price = price
self.quantity = quantity = self.price * self.quantity
Inventory.total_bill +=
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:”,

11 =Inventory(‘Soap’, 30.5, 4)
print(Inventory. __name__)


{‘ 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



(<class ‘object’>,)

Python Tutorial

Leave a Reply

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