Built-In Class Attributes in Python


In this article, we will explain to you the built-in class attributes in python

The built-in class attributes provide us with information about the class.

Using the dot (.) operator, we may access the built-in class attributes.

The built-in class attributes in python are listed below −

Attributes Description
__dict__ Dictionary containing the class namespace
__doc__ If there is a class documentation class, this returns it. Otherwise, None
__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.

Now we will see about each one of them in brief along with an example for better understanding

Example 1: __dict__ Class Attribute

Here we are printing the dict of the class.

# creating a class say Tutorialspoint class Tutorialspoint: 'Welcome to Tutorialspoint Python' def __init__(self): print("The __init__ method") # Printing the value of Built-in __dict__ attribute for the TutorialsPoint class print(Tutorialspoint.__dict__)

Output

On executing, the above program will generate the following output −

{'__module__': '__main__', '__doc__': 'Welcome to Tutorialspoint Python', '__init__': <function Tutorialspoint.__init__ at 0x7fd7812df050>, '__dict__': <attribute '__dict__' of 'Tutorialspoint' objects>, '__weakref__': <attribute '__weakref__' of 'Tutorialspoint' objects>}

Example 2: __doc__ Class Attribute

In the Python code below, we create a Tutorialspoint class with documentation.

# creating a class say Tutorialspoint class Tutorialspoint: 'Welcome to Tutorialspoint Python' def __init__(self): print("The __init__ method") # getting the documentation for the Tutorialspoint using # built-in __doc__ class attribute print(Tutorialspoint.__doc__)

Output

Welcome to Tutorialspoint Python

Example 3: __name__ Class Attribute

In the Python code below, we print the name of the class using the __name__ class attribute

class Tutorialspoint: 'Welcome to Tutorialspoint Python' def __init__(self): print("The __init__ method") # getting the name of the class using built-in __name__ class attribute print(Tutorialspoint.__name__)

Output

Tutorialspoint

Example 4: __module__ Class Attribute

In the Python code below, we print the module of the class using the __module__ class attribute

class Tutorialspoint: 'Welcome to Tutorialspoint Python' def __init__(self): print("The __init__ method") # getting the module of the class using built-in __module__ class attribute print(Tutorialspoint.__module__ )

Output

__main__

Example 5: __bases__ Class Attribute

In the Python code below, we print the bases of the class using the built-in __bases__ class attribute

class Tutorialspoint: 'Welcome to Tutorialspoint Python' def __init__(self): print("The __init__ method") # getting the bases of the class using built-in __bases__ class attribute print(Tutorialspoint.__bases__)

Output

(<class 'object'>,)

Applying all the class attributes in a single program

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a parent class with the name Employee

  • Create a variable and initiaize the variable with 0 for storing the employ count.

  • Create an init constructor which accepts the name, and salary as arguments.

  • In Python, __init__ is one of the reserved methods. It is referred to as a constructor in object-oriented programming. When an object is created from the class and access is necessary to initialize the class's attributes, the __init__ function is called.
    
  • Initialize the values of instance attributes

  • Increment the employ count by 1

  • Create a function with the name displayCount which prints the total employee count.

  • Print the employ count by applying the (.) operator on the Employee class

  • Create another function with the name displayEmployee and print name and salary.

  • Create a child class with the name subEmployee which inherits properties from the parent Employee class

  • Use __doc__ class attribute, to print the Employee class with documentation

  • Use __name__ class attribute, to print the name of the Employee class

  • Use the __module__ class attribute, to print the module of the Employee class

  • Use __dict__ class attribute, to print the dictionary containing the Employee class namespace

  • Use the __bases__ class attribute, to print all the base classes for the derived class.

Example

# Creating a parent class with the name Employee class Employee: 'Common base class for all employees' # initiaizing the variable with 0 for storing the employ count empCount = 0 # init constructor which accepts the name, salary as arguments def __init__(self, name, salary): # Initialize the values of instance attributes self.name = name self.salary = salary # incrementing the employ count by 1 Employee.empCount += 1 # displayCount () which prints the total employ count def displayCount(self): print("Total Employee :",Employee.empCount) # creating another function i.e, displayEmployee def displayEmployee(self): print ("Name : ", self.name, ", Salary: ", self.salary) # creating child class with the name subEmployee inheriting properties # from the parent Employee class class subEmployee(Employee): 'Derived class for Base - Employee Class' # printing Employee class with documentation using __doc__ class attribute print("Employee class documentation using the __doc__ attribute:\n", Employee.__doc__) # printing the name of the Employee class using __name__ class attribute print("Name of the class using the __name__ attribute:\n", Employee.__name__) # printing the module of the Employee class using __module__ class attribute print("Module of the class using the __module__ attribute:\n", Employee.__module__) # printing the dictionary containing the Employee class namespace # using __dict__ class attribute print("Dictionary containing the Employee class namespace using the __dict__ attribute:\n", Employee.__dict__) # printing all the base classes for the derived class using __bases__ class attribute print("Base classes of subEmployee class using the __bases__ attribute:\n", subEmployee.__bases__)

Output

Employee class documentation using the __doc__ attribute:
Common base class for all employees
Name of the class using the __name__ attribute:
Employee
Module of the class using the __module__ attribute:
__main__
Dictionary containing the Employee class namespace using the __dict__ attribute:
{'__module__': '__main__', '__doc__': 'Common base class for all employees', 'empCount': 0, '__init__': <function Employee.__init__ at 0x7f9532090050>, 'displayCount': <function Employee.displayCount at 0x7f95320900e0>, 'displayEmployee': <function Employee.displayEmployee at 0x7f9532090170>, '__dict__': <attribute '__dict__' of 'Employee' objects>, '__weakref__': <attribute '__weakref__' of 'Employee' objects>}
Base classes of subEmployee class using the __bases__ attribute:
(<class '__main__.Employee'>,)

Conclusion

We learned all five built-in class attributes with an example from this article. We also demonstrated how these five built-in class attributes are used in practice by using two classes.

Updated on: 22-Sep-2022

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements