Sequence Magic Methods

PythonPythonIntermediate
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/AdvancedTopicsGroup -.-> python/generators("`Generators`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/BasicConceptsGroup -.-> python/python_shell("`Python Shell`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-7845{{"`Sequence Magic Methods`"}} python/with_statement -.-> lab-7845{{"`Sequence Magic Methods`"}} python/variables_data_types -.-> lab-7845{{"`Sequence Magic Methods`"}} python/numeric_types -.-> lab-7845{{"`Sequence Magic Methods`"}} python/booleans -.-> lab-7845{{"`Sequence Magic Methods`"}} python/conditional_statements -.-> lab-7845{{"`Sequence Magic Methods`"}} python/for_loops -.-> lab-7845{{"`Sequence Magic Methods`"}} python/lists -.-> lab-7845{{"`Sequence Magic Methods`"}} python/tuples -.-> lab-7845{{"`Sequence Magic Methods`"}} python/function_definition -.-> lab-7845{{"`Sequence Magic Methods`"}} python/importing_modules -.-> lab-7845{{"`Sequence Magic Methods`"}} python/using_packages -.-> lab-7845{{"`Sequence Magic Methods`"}} python/classes_objects -.-> lab-7845{{"`Sequence Magic Methods`"}} python/constructor -.-> lab-7845{{"`Sequence Magic Methods`"}} python/polymorphism -.-> lab-7845{{"`Sequence Magic Methods`"}} python/encapsulation -.-> lab-7845{{"`Sequence Magic Methods`"}} python/generators -.-> lab-7845{{"`Sequence Magic Methods`"}} python/data_collections -.-> lab-7845{{"`Sequence Magic Methods`"}} python/python_shell -.-> lab-7845{{"`Sequence Magic Methods`"}} python/build_in_functions -.-> lab-7845{{"`Sequence Magic Methods`"}} end

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.

Other Python Tutorials you may like