Python Special Methods and Object Behavior
Learn how Python classes customize built-in operations with special methods such as __repr__, __str__, __format__, __len__, __bool__, __contains__, __eq__, and __add__.
Intermediate
Python lets a class participate in built-in operations by defining methods with special names. You do not normally call these methods yourself; Python calls the appropriate hook when code uses syntax such as str(obj), len(obj), obj == other, or obj + other.
Special methods are language hooks
A special method is a method whose name follows Python's double-underscore convention and whose meaning is defined by the language. The useful mental model is simple: your class supplies a hook, and ordinary Python syntax triggers that hook.
|
|
The ordinary repr() call triggers the class's __repr__ hook.
Choose a useful __repr__
__repr__ defines the representation returned by repr(). The official data model describes it as the object's official representation and recommends making it information-rich and unambiguous. It should return a string. A common goal is to make the result useful while debugging.
|
|
repr() can expose enough state to make a value easy to inspect while debugging.
Make printing readable with __str__
__str__ defines the informal string representation used by str() and print(). It is usually more convenient or concise than __repr__. If a class has no __str__, Python can fall back to __repr__ for the informal representation.
|
|
print() and str() use the object's __str__ method for a user-facing representation.
Customize formatted output with __format__
__format__ defines how format(obj, format_spec), formatted string literals, and str.format() produce a formatted string for an object. The format specification is a string; the class decides how to interpret it. Keep this hook distinct from __str__, which supplies the ordinary informal representation. A custom __format__ method must return a string.
|
|
The class handles its own currency format and delegates ordinary numeric format specifications to the stored float.
The default __format__ implementation inherited from object accepts an empty format specification and uses __str__(); it raises TypeError for a non-empty specification. Defining __format__ makes your class responsible for handling the formats it chooses to support. The Format Specification lesson remains the owner of the standard format-specification mini-language.
Customize binary conversion with __bytes__
__bytes__() defines the binary representation returned by bytes(obj). The method must return a bytes object; it is not a replacement for text encoding, which should specify an encoding explicitly. Choose and document a stable byte layout for your class, and keep its values within the byte range when building the result from integers.
|
|
The class defines a two-byte representation for its status and value fields. The bytes constructor calls __bytes__() and receives bytes, not text.
Do not confuse bytes(obj) with encoding a string: str.encode() converts text using a chosen character encoding, while __bytes__() lets a class define its own binary representation. The method must return bytes; returning str, bytearray, or another type raises TypeError.
Quick check
Which expression asks the custom object for its binary representation?
class Packet:
def __bytes__(self):
return b"\x07\x03"
def __str__(self):
return "packet text"
def __repr__(self):
return "Packet()"
packet = Packet()
print()
Give objects a length with __len__
__len__ defines the result of len(obj). The method must return a non-negative integer. Once a class provides it, the object can participate naturally in APIs that ask for a length.
|
|
Defining __len__ makes len(playlist) delegate to the class's length hook.
Control truth testing with __bool__
__bool__ defines how an object behaves in a Boolean context and when passed to bool(). It must return True or False. If __bool__ is absent, Python can use __len__ instead: a zero length is false and a nonzero length is true.
|
|
A class can define its own truth rule instead of relying on object length.
Customize membership with __contains__
__contains__ controls membership tests such as item in obj. It lets a class define what membership means for itself. This is different from the iterator protocol taught in the Iterators lesson, which can provide a fallback when __contains__ is not defined.
|
|
The in operator calls the class's membership hook when __contains__ is defined.
Customize equality with __eq__
__eq__ defines the behavior of ==. A simple implementation can compare the relevant attributes and return a Boolean. For more general classes, a comparison method can return NotImplemented when it does not support the other operand; Python can then try the appropriate fallback.
|
|
The == operator can compare object values instead of default identity-based equality.
Customize + with __add__
__add__ defines the behavior of the + operator when your object is on the left. The method receives the other operand and should return the result, or NotImplemented when the operation is not supported for that operand type.
|
|
Defining __add__ lets ordinary + syntax create a result appropriate for the class.
Special methods work together
Real classes often define several special methods that describe different aspects of the same object. Keep each method focused on the operation it customizes. You do not need to implement a large collection of hooks: add the ones that make the class behave naturally.
|
|
Several hooks can describe one object naturally without changing how callers use ordinary Python syntax.
Keep advanced hooks separate
Python has many more special methods for attribute access, descriptors, class creation, numeric operations, subscriptions, pattern matching, and other protocols. This lesson establishes the core idea and the most useful object-facing hooks. The existing Iterators lesson remains the owner of __iter__() and __next__(); advanced protocols deserve separate lessons when they are justified.
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.