Python
Magic Attributes
Intermediate
Magic attributes, also known as dunder attributes, are special variables that Python automatically creates and attaches to modules, classes, functions, and objects.
They provide important information such as the name of a class, its documentation, parent classes, method resolution order, and more. Understanding these attributes helps you inspect, debug, and work more effectively with Python objects.
Magic Attributes (Dunder Attributes)
While magic methods (dunder methods) define behavior, magic attributes (dunder attributes) provide metadata and internal information about objects, classes, functions, and modules.
Python has 50+ built-in dunder attributes. You usually don’t implement most of them yourself — Python sets them automatically. However, understanding them is essential for introspection, debugging, and metaprogramming.
Most Important Magic Attributes
|
Attribute
|
Belongs To
|
Purpose
|
|---|---|---|
|
__name__
|
Modules, Classes, Functions
|
Name of the module, class, or function
|
|
__doc__
|
Modules, Classes, Functions, Methods
|
Docstring (documentation string)
|
|
__module__
|
Classes, Functions
|
Name of the module where it was defined
|
|
__class__
|
Instances
|
The class of the object (
type(obj)
)
|
|
__dict__
|
Instances, Classes, Modules
|
Dictionary containing writable attributes
|
|
__bases__
|
Classes
|
Tuple of base (parent) classes
|
|
__mro__
|
Classes
|
Method Resolution Order (tuple)
|
|
__qualname__
|
Classes, Functions
|
Qualified name (e.g.
Class.method
)
|
|
__annotations__
|
Functions, Classes
|
Dictionary of type hints
|
|
__slots__
|
Classes
|
Memory optimization by restricting attributes
|
|
__file__
|
Modules
|
Full path to the module’s source file
|
|
__package__
|
Modules
|
Package name (for packages and sub-packages)
|
|
__spec__
|
Modules
|
Module spec used by the import system
|
class MagicDemo:
"""This is a demo class to showcase magic attributes."""
class_var = "Hello"
def __init__(self, value):
self.instance_var = value
def method(self):
"""A sample method."""
pass
# Usage
obj = MagicDemo(42)
print("=== Object ===")
print(obj.__class__) # <class '__main__.MagicDemo'>
print(obj.__dict__) # {'instance_var': 42}
print("\n=== Class ===")
print(MagicDemo.__name__) # MagicDemo
print(MagicDemo.__module__) # __main__
print(MagicDemo.__qualname__) # MagicDemo
print(MagicDemo.__doc__) # This is a demo class...
print(MagicDemo.__bases__) # (<class 'object'>,)
print(MagicDemo.__mro__) # (<class '__main__.MagicDemo'>, <class 'object'>)
print("\n=== Function ===")
print(MagicDemo.method.__name__) # method
print(MagicDemo.method.__qualname__) # MagicDemo.method
print(MagicDemo.method.__doc__) # A sample method.
print("\n=== Module ===")
import os
print(os.__name__) # os
print(os.__file__) # /path/to/os.py
print(os.__package__) # ''
Magic attributes give you direct access to metadata about your code. The most commonly used ones include __name__, __doc__, __class__, __dict__, __bases__, __mro__, and __module__.
While you rarely need to create your own dunder attributes, knowing how to read and use them is an essential skill for writing clean, introspective, and professional Python code.
Mastering both magic methods and magic attributes will significantly improve how you design and understand Python classes.
Classes Magic Attributes OOP Objects Python