Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
To create custom sequences in Python, you can define a class and overload specific operators using special methods. For sequences, the most important operators to overload are:
__getitem__(self, index)
: To get an item at a specific index.__setitem__(self, index, value)
: To set the value of an item at a specific index (for mutable sequences).__len__(self)
: To get the length of the sequence.__delitem__(self, index)
: To delete an item at a specific index (for mutable sequences).__iter__(self)
: To create an iterator for the sequence.In this example, we'll create a custom sequence called MyList
that stores its items in a list:
class MyList: def __init__(self, items=None): self._items = list(items) if items else [] def __getitem__(self, index): return self._items[index] def __setitem__(self, index, value): self._items[index] = value def __len__(self): return len(self._items) def __delitem__(self, index): del self._items[index] def __iter__(self): return iter(self._items) def __repr__(self): return f"MyList({self._items})"
Now, you can create an instance of MyList
and use the overloaded operators to interact with the custom sequence:
my_list = MyList([1, 2, 3, 4, 5]) print(my_list[2]) # Output: 3 print(len(my_list)) # Output: 5 my_list[1] = 99 print(my_list) # Output: MyList([1, 99, 3, 4, 5]) del my_list[3] print(my_list) # Output: MyList([1, 99, 3, 5]) for item in my_list: print(item)
In this example, we have implemented the special methods __getitem__
, __setitem__
, __len__
, __delitem__
, and __iter__
to overload the relevant operators for our custom MyList
sequence. This allows us to perform sequence operations like indexing, setting values, finding the length, deleting items, and iterating over the sequence in a way that is both intuitive and specific to our custom sequence class.
Python custom sequence implementation with operator overloading:
class CustomSequence: def __init__(self, data): self.data = data def __getitem__(self, index): return self.data[index] def __len__(self): return len(self.data) my_sequence = CustomSequence([1, 2, 3, 4, 5]) print(my_sequence[2]) # Output: 3
Operator overloading for custom sequence in Python example:
class CustomSequence: def __init__(self, data): self.data = data def __getitem__(self, index): return self.data[index] def __len__(self): return len(self.data) def __add__(self, other): if isinstance(other, CustomSequence): return CustomSequence(self.data + other.data) else: raise TypeError("Unsupported operand type") seq1 = CustomSequence([1, 2, 3]) seq2 = CustomSequence([4, 5, 6]) result = seq1 + seq2 print(result.data) # Output: [1, 2, 3, 4, 5, 6]
Creating iterable objects in Python using operator overloading:
class CustomIterable: def __init__(self, data): self.data = data def __iter__(self): return iter(self.data) my_iterable = CustomIterable([1, 2, 3, 4, 5]) for item in my_iterable: print(item)
Custom sequence manipulation with Python operator overloads:
Example with custom slicing:
class CustomSequence: def __init__(self, data): self.data = data def __getitem__(self, index): if isinstance(index, slice): return CustomSequence(self.data[index]) else: return self.data[index] def __len__(self): return len(self.data) my_sequence = CustomSequence([1, 2, 3, 4, 5]) sliced_sequence = my_sequence[1:4] print(sliced_sequence.data) # Output: [2, 3, 4]
Implementing custom containers with operator overloading in Python:
class CustomContainer: def __init__(self): self.data = [] def __contains__(self, item): return item in self.data def add_item(self, item): self.data.append(item) my_container = CustomContainer() my_container.add_item(42) print(42 in my_container) # Output: True
Building custom iterable classes with Python operator overloads:
class CustomIterable: def __init__(self, data): self.data = data def __iter__(self): return iter(self.data) def __add__(self, other): if isinstance(other, CustomIterable): return CustomIterable(self.data + other.data) else: raise TypeError("Unsupported operand type") iterable1 = CustomIterable([1, 2, 3]) iterable2 = CustomIterable([4, 5, 6]) result = iterable1 + iterable2 for item in result: print(item)
Creating custom list-like objects with Python operator overloading:
class CustomList: def __init__(self): self.data = [] def __getitem__(self, index): return self.data[index] def __setitem__(self, index, value): self.data[index] = value def __len__(self): return len(self.data) my_list = CustomList() my_list[0] = 42 print(my_list[0]) # Output: 42
Implementing custom iterators with operator overloading in Python:
class CustomIterator: def __init__(self, data): self.data = data self.index = 0 def __iter__(self): return self def __next__(self): if self.index < len(self.data): result = self.data[self.index] self.index += 1 return result else: raise StopIteration my_iterator = CustomIterator([1, 2, 3, 4, 5]) for item in my_iterator: print(item)
Sequence slicing and indexing with operator overloading in Python:
class CustomSequence: def __init__(self, data): self.data = data def __getitem__(self, index): if isinstance(index, slice): return CustomSequence(self.data[index]) else: return self.data[index] def __len__(self): return len(self.data) my_sequence = CustomSequence([1, 2, 3, 4, 5]) sliced_sequence = my_sequence[1:4] print(sliced_sequence.data) # Output: [2, 3, 4]