Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22852

Real Python: Custom Python Lists: Inheriting From list vs UserList

$
0
0

At some point in your Python coding adventure, you may need to create custom list-like classes with modified behavior, new functionalities, or both. To do this in Python, you can inherit from an abstract base class, subclass the built-in list class directly, or inherit from UserList, which lives in the collections module.

In this tutorial, you’ll learn how to:

  • Create custom list-like classes by inheriting from the built-in list class
  • Build custom list-like classes by subclassing UserList from the collections module

You’ll also write some examples that’ll help you decide which parent class, list or UserList, to use when creating your custom list classes.

To get the most out of this tutorial, you should be familiar with Python’s built-in list class and its standard features. You’ll also need to know the basics of object-oriented programming and understand how inheritance works in Python.

Free Download:Click here to download the source code that you’ll use to create custom list-like classes.

Creating List-Like Classes in Python

The built-in list class is a fundamental data type in Python. Lists are useful in many situations and have tons of practical use cases. In some of these use cases, the standard functionality of Python list may be insufficient, and you may need to create custom list-like classes to address the problem at hand.

You’ll typically find at least two reasons for creating custom list-like classes:

  1. Extending the regular list by adding new functionality
  2. Modifying the standard list’s functionality

You can also face situations in which you need to both extend and modify the list’s standard functionality.

Depending on your specific needs and skill level, you can use a few strategies to create your own custom list-like classes. You can:

Note: In object-oriented programming, it’s common practice to use the verbs inherit and subclass interchangeably.

There are a few considerations when you’re selecting the appropriate strategy to use. Keep reading for more details.

Building a List-Like Class From an Abstract Base Class

You can create your own list-like classes by inheriting from an appropriate abstract base class (ABC), like MutableSequence. This ABC provides generic implementations of most list methods except for .__getitem__(), .__setitem__(), .__delitem__, .__len__(), and .insert(). So, when inheriting from this class, you’ll have to implement these methods yourself.

Writing your own implementation for all these special methods is a fair amount of work. It’s error-prone and requires advanced knowledge of Python and its data model. It can also imply performance issues because you’ll be writing the methods in pure Python.

Additionally, suppose you need to customize the functionality of any other standard list method, like .append() or .insert(). In that case, you’ll have to override the default implementation and provide a suitable implementation that fulfills your needs.

The main advantage of this strategy for creating list-like classes is that the parent ABC class will alert you if you miss any required methods in your custom implementation.

In general, you should embrace this strategy only if you need a list-like class that’s fundamentally different from the built-in list class.

In this tutorial, you’ll focus on creating list-like classes by inheriting from the built-in list class and the UserList class from the standard-library collections module. These strategies seem to be the quickest and most practical ones.

Inheriting From Python’s Built-in list Class

For a long time, it was impossible to inherit directly from Python types implemented in C. Python 2.2 fixed this issue. Now you can subclass built-in types, including list. This change has brought several technical advantages to the subclasses because now they:

Read the full article at https://realpython.com/inherit-python-list/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]


Viewing all articles
Browse latest Browse all 22852

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>