Open In App

Built-In Class Attributes In Python

Last Updated : 15 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Python offers many tools and features to simplify software development. Built-in class attributes are the key features that enable developers to provide important information about classes and their models. These objects act as hidden gems in the Python language, providing insight into the structure and behavior of objects. In this article, we will learn about built-in class attributes in Python.

Built-In Class Attributes in Python

In Python, built-in class attributes are properties associated with a class itself, rather than its instances. Common built-in class attributes include __doc__, which holds the class documentation string, and __name__, which stores the class name. __module__ indicates the module in which the class is defined, and __bases__ holds a tuple of the base classes. These attributes provide metadata about the class and are accessible.

__doc__ Attribute in Python

The __doc__ attribute contains the string of the class documents. Documentation strings, often called docstrings, are used to describe what a class or function does. Docstrings are enclosed in triple quotes (”” or “””) and are usually placed immediately after the class or function definition.

Syntax:

print(ClassName.__doc__) 

In the below example, the GeeksCourse class has a docstring providing information about the programming course. By printing GeeksCourse.__doc__, the docstring is accessed and displayed.

Python3




class GeeksCourse:
    """
     This class represents a programming course on GeeksforGeeks.
     It covers various topics and provides valuable resources for learners.
    """
 
# Access and print the docstring using __doc__ attribute
print(GeeksCourse.__doc__)


Output

     This class represents a programming course on GeeksforGeeks.
     It covers various topics and provides valuable resources for learners.
    

__name__ Attribute in Python

The __name__ is a special attribute in Python that provides the name of the class to which it belongs. It is used to retrieve the name of a class within the class definition or in instances of the class.

Syntax:

print(ClassName.__name__)

In the below example, the class Geeks is defined with a docstring. By printing Geeks.__name__, the __name__ attribute is accessed, and it outputs the name “Geeks.”

Python3




class Geeks:
    """
      Example Class
    """
# Access and print the name using __name__ attribute
print(Geeks.__name__)


Output

Geeks

__module__ Attribute in Python

The __module__ attribute in Python represents the name of the module in which a class is defined. It allows you to access and retrieve the module name to which a particular class belongs.__module__ ` contains the name of that module. This attribute is especially useful when working with courses defined in modules.

Syntax:

print(ClassName.__module__)

In the below example, the class Geeks is defined with a docstring. By printing Geeks.__module__, the __module__ attribute is accessed, and it outputs the name of the module in which the class is defined.

Python3




class Geeks:
    """
     Class
    """
 
# Access and print the module using __module__ attribute
print(Geeks.__module__)


Output

__main__

__bases__ Attribute in Python

The __bases__ attribute in Python is used to access a tuple containing the base classes of a class. It provides information about the direct parent classes from which the class inherits.

Syntax:

print(ClassName.__bases__)

In the below example, the class Geeks is defined with a docstring. By printing Geeks.__bases__, the __bases__ attribute is accessed. However, this attribute is typically used with derived classes and will output an empty tuple for the base classes of the Geeks class since it doesn’t inherit from any other class.

Python3




class Geeks:
    """
     bases
    """
 
# Access and print the base using __bases__ attribute
print(Geeks.__bases__)


Output

(<class 'object'>,)

__dict__ Attribute in Python

The __dict__ attribute in Python is a dictionary containing the namespace of a class or instance. It holds the attributes and methods defined for the class or object, allowing dynamic inspection and manipulation of its members.

Syntax:

print(ClassName.__dict__)

In this example, the class Geeks is defined with a docstring. By printing Geeks.__dict__, the __dict__ attribute is accessed, and it outputs a dictionary containing the namespace of the class. This dictionary represents the attributes and methods defined within the class.

Python3




class Geeks:
    """
     dict
    """
 
# Access and print the dict using __dict__ attribute
print(Geeks.__dict__)


Output

{'__module__': '__main__', '__doc__': '\n     dict\n    ', '__dict__': <attribute '__dict__' of 'Geeks' objects>, '__weakref__': <attribute '__weakref__' of 'Geeks' objects>}

Conclusion

In conclusion, built-in class attributes in Python, such as doc, name, module, bases, and dict, serve as invaluable tools for understanding and working with classes. These attributes offer metadata about class structures, documentation, and inheritance, providing insights into the object-oriented design. Leveraging these attributes enhances code readability, documentation practices, and dynamic manipulation of class members.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads