The collections.UserList
class in Python’s collections
module provides a wrapper around list objects. It is a useful base class for creating custom list-like objects by subclassing and modifying its behavior.
Table of Contents
- Introduction
collections.UserList
Class Syntax- Examples
- Basic Usage
- Overriding Methods
- Adding Custom Methods
- Real-World Use Case
- Conclusion
Introduction
The collections.UserList
class provides a convenient way to create custom list-like objects by inheriting from it and overriding or extending its methods. This class simplifies the process of creating specialized lists without directly subclassing the built-in list
class.
collections.UserList Class Syntax
Here is how you use the collections.UserList
class:
from collections import UserList
class MyList(UserList):
pass
Parameters:
- The
UserList
class itself doesn’t take any special parameters. When subclassing it, you can define your custom initialization and methods.
Returns:
- A new UserList or a subclass of UserList object.
Examples
Basic Usage
Here is an example of how to create and use a custom list by subclassing UserList
.
Example
from collections import UserList
# Creating a custom list by subclassing UserList
class MyList(UserList):
pass
# Creating an instance of MyList
my_list = MyList([1, 2, 3, 4, 5])
print(my_list)
Output:
[1, 2, 3, 4, 5]
Overriding Methods
You can override existing methods of the list to customize the behavior of your custom list.
Example
from collections import UserList
# Creating a custom list with overridden append method
class MyList(UserList):
def append(self, item):
print(f"Appending {item}")
super().append(item)
# Creating an instance of MyList
my_list = MyList([1, 2, 3])
my_list.append(4)
my_list.append(5)
print(my_list)
Output:
Appending 4
Appending 5
[1, 2, 3, 4, 5]
Adding Custom Methods
You can add custom methods to your subclass to extend its functionality.
Example
from collections import UserList
# Creating a custom list with an additional method
class MyList(UserList):
def increment_all(self, amount):
for i in range(len(self)):
self[i] += amount
# Creating an instance of MyList
my_list = MyList([1, 2, 3])
my_list.increment_all(5)
print(my_list)
Output:
[6, 7, 8]
Real-World Use Case
Validated List
A common real-world use case for subclassing UserList
is creating a validated list where elements are validated before being added.
Example
from collections import UserList
# Creating a validated list that only accepts integers
class IntegerList(UserList):
def append(self, item):
if not isinstance(item, int):
raise TypeError("Only integers are allowed")
super().append(item)
def extend(self, other):
for item in other:
self.append(item)
# Creating an instance of IntegerList
int_list = IntegerList([1, 2, 3])
int_list.append(4)
try:
int_list.append('a')
except TypeError as e:
print(e)
print(int_list)
Output:
Only integers are allowed
[1, 2, 3, 4]
Conclusion
The collections.UserList
class in Python’s collections
module provides a flexible base class for creating custom list-like objects. By subclassing UserList
, you can easily override or extend its methods to tailor the behavior of your custom lists to suit specific needs. Proper usage of this class can simplify the process of creating specialized lists and enhance the readability and functionality of your code.