Introduction
In this tutorial, we will cover the sequence magic methods in Python. These methods allow you to customize the behavior of your own classes when used in different operations, such as getting the length of an object, accessing items, slicing, and iteration.
Length and Containment
Length and containment methods are special methods in Python that allow objects to define their behavior when used with built-in functions or operators such as len(), in, and not in.
Let's start with a simple object. In sequence.py, create a class named MySequence that have an attribute data.
class MySequence:
def __init__(self, data: list):
"""Initialize a new MySequence object with the given data."""
self.data = data
__len__
The __len__ magic method is used to define the behavior of the len() function when applied to instances of your class.
## ... (previous code in sequence.py)
def __len__(self) -> int:
"""Return the length of the sequence."""
return len(self.data)
__contains__
The __contains__ magic method is used to define the behavior of the in and not in operators when applied to instances of your class.
## ... (previous code in sequence.py)
def __contains__(self, item: object) -> bool:
"""Check if the sequence contains the given item."""
return item in self.data
Example: Using the Length and Containment Methods
Now that we have defined the length and containment methods for our MySequence class, let's see how they work in length_containment_example.py:
from sequence import MySequence
## Create a MySequence object
my_seq = MySequence([1, 2, 3, 4, 5])
## Test the __len__ magic method
print(len(my_seq)) ## Output: 5
## Test the __contains__ magic method
print(3 in my_seq) ## Output: True
print(3 not in my_seq) ## Output: False
print(10 in my_seq) ## Output: False
Then typing the following command in the terminal to execute the script.
python length_containment_example.py
Item Access
Item access methods are special methods in Python that allow objects to define their behavior when accessed using square bracket notation [].
__getitem__
The __getitem__ magic method is used to define the behavior of the square bracket notation for accessing items in instances of your class.
## ... (previous code in sequence.py)
def __getitem__(self, index: int) -> object:
"""Return the item at the given index."""
return self.data[index]
__setitem__
The __setitem__ magic method is used to define the behavior of the square bracket notation for setting items in instances of your class.
## ... (previous code in sequence.py)
def __setitem__(self, index: int, value: object) -> None:
"""Set the item at the given index to the given value."""
self.data[index] = value
__delitem__
The __delitem__ magic method is used to define the behavior of the del keyword when applied to instances of your class.
## ... (previous code in sequence.py)
def __delitem__(self, index: int) -> None:
"""Remove the item at the given index."""
del self.data[index]
Example: Using the Item Access Methods
Now that we have defined the item access methods for our MySequence class, let's see how they work in item_access_example.py:
from sequence import MySequence
## Create a MySequence object
my_seq = MySequence([1, 2, 3, 4, 5])
## Test the __getitem__ magic method
print(my_seq[2]) ## Output: 3
## Test the __setitem__ magic method
my_seq[2] = 9
print(my_seq[2]) ## Output: 9
## Test the __delitem__ magic method
del my_seq[2]
print(len(my_seq)) ## Output: 4
print(my_seq.data) ## Output: [1, 2, 4, 5]
Then typing the following command in the terminal to execute the script.
python item_access_example.py
Iteration
Iteration methods are special methods in Python that allow objects to define their behavior when used with iteration protocols.
__iter__
The __iter__ magic method is used to define the behavior of the iter() function and the for ... in ... loop when applied to instances of your class.
## ... (previous code in sequence.py)
def __iter__(self):
"""Return an iterator for the sequence."""
for item in self.data:
yield item
__reversed__
The __reversed__ magic method is used to define the behavior of the reversed() function when applied to instances of your class.
## ... (previous code in sequence.py)
def __reversed__(self):
"""Return a reversed iterator for the sequence."""
return reversed(self.data)
Example: Using the Iteration Methods
Now that we have defined the iteration methods for our MySequence class, let's see how they work in iteration_example.py:
from sequence import MySequence
## Create a MySequence object
my_seq = MySequence([1, 2, 3, 4, 5])
## Test the __iter__ magic method
for item in my_seq:
print(item, end=" ") ## Output: 1 2 4 5
## Test the __reversed__ magic method
for item in reversed(my_seq):
print(item, end=" ") ## Output: 5 4 2 1
Then typing the following command in the terminal to execute the script.
python iteration_example.py
Summary
With the implemented magic methods, our custom MySequence class now behaves similarly to built-in sequence types such as lists and tuples. You can now use it as if it were a native Python sequence.



