Python – Data Model
In Python, everything is an object — from integers and strings to functions and classes.
But have you ever wondered:
-
How does
+work on objects? -
Why does
len()work on lists? -
How does
print()know what to display?
The answer lies in the Python Data Model.
What is the Python Data Model?
The Python Data Model defines:
How objects behave in Python and how built-in operations interact with them.
It is implemented using special methods, also called magic methods or dunder methods (double underscore methods).
What are Magic Methods?
Magic methods are special functions that start and end with double underscores:
Examples:
-
__init__ -
__str__ -
__add__ -
__len__
They allow you to customize object behavior.
Example: Basic Data Model
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p = Person("Alice", 25)
print(p)
Output:
<__main__.Person object at 0x...>
✔ Not user-friendly yet
Making Objects Readable: __str__
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"{self.name} is {self.age} years old"
p = Person("Alice", 25)
print(p)
Output:
Alice is 25 years old
Internal Object Representation: __repr__
class Person:
def __repr__(self):
return "Person(name='Alice', age=25)"
✔ Used for debugging and logging
Operator Overloading Example
Adding Objects with __add__
class Number:
def __init__(self, value):
self.value = value
def __add__(self, other):
return Number(self.value + other.value)
def __str__(self):
return str(self.value)
a = Number(10)
b = Number(20)
print(a + b)
Output:
30
How Python Executes +
When you write:
a + b
Python internally calls:
a.__add__(b)
Length of Objects: __len__
class MyList:
def __init__(self, items):
self.items = items
def __len__(self):
return len(self.items)
obj = MyList([1, 2, 3, 4])
print(len(obj))
Output:
4
Indexing Support: __getitem__
class MyList:
def __init__(self, items):
self.items = items
def __getitem__(self, index):
return self.items[index]
obj = MyList([10, 20, 30])
print(obj[1])
Output:
20
Assignment Behavior: __setitem__
class MyList:
def __init__(self, items):
self.items = items
def __setitem__(self, index, value):
self.items[index] = value
obj = MyList([1, 2, 3])
obj[1] = 99
print(obj.items)
Output:
[1, 99, 3]
Attribute Access: __getattr__
class MyClass:
def __getattr__(self, name):
return f"{name} not found"
obj = MyClass()
print(obj.age)
Output:
age not found
Why Python Data Model is Powerful
It allows you to:
- Customize object behavior
- Overload operators
- Build clean APIs
- Create framework-level abstractions
Real-World Usage
The Python Data Model is heavily used in:
- ORM frameworks (Django, SQLAlchemy)
- Data classes
- Custom containers
- API frameworks
- Built-in types like lists, dicts, str
All built on Python internals.
Example: Built-in Data Model Behavior
len([1, 2, 3])
Internally:
list.__len__()
Advantages
- Full control over object behavior
- Clean and readable APIs
- Enables operator overloading
- Makes Python highly flexible
Limitations
- Can make code complex
- Hard to debug if overused
- Requires deep understanding
Summary
The Python Data Model defines how objects behave internally using special methods like __init__, __str__, and __add__.
It is the foundation of Python’s object system and is widely used in advanced programming with Python.
Conclusion
Understanding the Data Model is essential for mastering object-oriented programming in Python.
It allows you to build powerful, flexible, and Pythonic APIs that integrate deeply with the language itself.


0 Comments