Getting Started

Python - IntroductionPython - Hello World ProgramPython - SyntaxPython - Data TypesPython - Variables

Operators

Python - Arithmetic OperatorsPython - Relational OperatorsPython - Logical OperatorsPython - Assignment OperatorsPython - Bitwise OperatorsPython - Membership OperatorsPython - Identity OperatorsPython - Increment and Decrement Operators

Conditions

Python - If Else statement

Loop

Python - While LoopPython - For Loop

Numbers

Python - NumbersPython - Number Conversion

Strings

Python - StringsPython - String OperatorsPython - String FormattingPython - String MethodsPython - String Format Method

List

Python - ListPython - List Methods

Tuple

Python - Tuple

Set

Python - SetPython - Set Methods

Dictionary

Python - DictionaryPython - Dictionary Methods

Functions

Python - FunctionsPython - Functions - Variable length argumentsPython - Lambda Function

Scope of Variables

Python - Scope of Variables

Modules

Python - ModulesPython - Math ModulePython - JSON ModulePython - datetime ModulePython - time Module

I/O

Python - Read input from keyboard

File

Python - File Handling

Exception Handling

Python - Exception Handling

OOP

Python - Classes and ObjectsPython - Class Constructor __init__ methodPython - Class Destructor __del__ methodPython - Built-in Class AttributesPython - InheritancePython - Method OverridingPython - Method Overloading

Package Management

Python - PIP

Python - MySQL

Python - MySQL - Getting StartedPython - MySQL - Insert dataPython - MySQL - Select dataPython - MySQL - Update dataPython - MySQL - Delete data

Python - CSV

Python - Read data from CSV filePython - Write data in CSV file

Python - Built-in Class Attributes

Python

python logo

In this tutorial we will learn about built-in class attributes in Python.

Built-in class attributes gives us information about the class.

We can access the built-in class attributes using the . operator.

Following are the built-in class attributes.

AttributeDescription
__dict__This is a dictionary holding the class namespace.
__doc__This gives us the class documentation if documentation is present. None otherwise.
__name__This gives us the class name.
__module__This gives us the name of the module in which the class is defined.

In an interactive mode it will give us __main__.

__bases__A possibly empty tuple containing the base classes in the order of their occurrence.

The __doc__ class attribute

In the following Python program we are creating Awesome class with documentation.

# class
class Awesome:
    'This is a sample class called Awesome.'

    def __init__(self):
        print("Hello from __init__ method.")

# class built-in attribute
print(Awesome.__doc__)

The above code will give us the following output.

This is a sample class called Awesome.

The __name__ class attribute

In the following example we are printing the name of the class.

# class
class Awesome:
    'This is a sample class called Awesome.'

    def __init__(self):
        print("Hello from __init__ method.")

# class built-in attribute
print(Awesome.__name__)

Output:

Awesome

The __module__ class attribute

In the following example we are printing the module of the class.

# class
class Awesome:
    def __init__(self):
        print("Hello from __init__ method.")

# class built-in attribute
print(Awesome.__module__)

Output:

__main__

The __bases__ class attribute

In the following example we are printing the bases of the class.

# class
class Awesome:
    def __init__(self):
        print("Hello from __init__ method.")

# class built-in attribute
print(Awesome.__bases__)

Output:

(<class 'object'>,)

The __dict__ class attribute

In the following example we are printing the dict of the class.

# class
class Awesome:
    def __init__(self):
        print("Hello from __init__ method.")

# class built-in attribute
print(Awesome.__dict__)

Output:

{'__module__': '__main__', '__doc__': 'Awesome class sample documentation.', '__init__': <function Awesome.__init__ at 0x106e2c1e0>, '__dict__': <attribute '__dict__' of 'Awesome' objects>, '__weakref__': <attribute '__weakref__' of 'Awesome' objects>}

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT