How to get and print Python class Attributes

Python OOP-Python


In this article, we are going to learn How to get and print Python class Attributes . The built-in attributes of a Python class and Python class Attribute how to get and print. When we declare a Python class, it comes with some built-in attributes that we get by default. These built-in attributes of Python classes are very useful and provide us a lot of information about that class. All these attributes have special representation and they all start with the double underscore “__”.

So let us start learning about these Python class Attributes in detail with the help of code examples.

First of all, let us see what are the attributes that we get in Python classes:

List of attribute in Python classes


1. __dict__ Dictionary

The Dictionary attribute of Python classes contains the information about the namespace to which this class belongs. Namespaces are the collections or modules scope definition that can include many classes inside it. So with the help of the Dictionary attribute, we can get this information about the class.

2. __doc__ Class documentation

Class documentation attribute gives us the class string doc information to us. This attribute gives us this information if this was defined by the person who wrote the class. If there is no string available in this then it returns none. In the class definition, class documentation is the very first line. This is not a mandatory requirement in class definition but it is good practice to have this. This helps others to understand what this class is about and what kind of functionality this class implements.

3.__name__ Class name

Class name attribute gives us the name of the class.

4. __module__ Module name

Module name attribute tells us about in which module the class is defined. When we work in interactive mode then this attribute returns “main“.

5.__bases__ Base class list

Inheritance is a very useful feature of OOPs and it provides us the concept of Base and derived classes. Base class list attributes help us to find out the Base class details of any class. It provides us this information as a tuple containing the base classes, in the order of their occurrence in the base class list. The object class is the base of all the classes in Python so this will return the object even if there is no base class defined by you.

Now let us jump to a Code example and demo of each of these class attributes. To start with, let us first define a class that we will use to understand all these attributes.

Program to get and print Python class Attributes

class Student:

   'this is student class'
   
   studentcount = 0

   def __init__(self, name, standard):
      self.name = name
      self.standard = standard
      Student.studentcount += 1
   
   def displayStuCount(self):
     print ("Total Students %d" % Student.studentcount)

   def displayEmployee(self):
      print ("StuName : ", self.name,  ", standard: ",self.standard)

      
stud1 = Student("max", 5)

stud2 = Student("Rack", 10)

print ("Student.__doc__:", Student.__doc__)

print ("Student.__name__:", Student.__name__)

print ("Student.__module__:", Student.__module__)

print ("Student.__bases__:", Student.__bases__)

print ("\nStudent.__dict__:\n", Student.__dict__)



Output

Student.__doc__: this is student class
Student.__name__: Student
Student.__module__: __main__
Student.__bases__: (<class 'object'>,)

Student.__dict__:
 {'__module__': '__main__', '__doc__': 'this is student class', 'studentcount': 2, '__init__': <function Student.__init__ at 0x03D56190>, 'displayStuCount': <function Student.displayStuCount at 0x03D561D8>, 'displayEmployee': <function Student.displayEmployee at 0x03D56220>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>}