Custom Python Strings: Inheriting From str vs UserString :

Custom Python Strings: Inheriting From str vs UserString
blow post content copied from  Real Python
click here to view original post

The Python str class has many useful features that can help you out when you’re processing text or strings in your code. However, in some situations, all these great features may not be enough for you. You may need to create custom string-like classes. To do this in Python, you can inherit from the built-in str class directly or subclass UserString, which lives in the collections module.

In this tutorial, you’ll learn how to:

  • Create custom string-like classes by inheriting from the built-in str class
  • Build custom string-like classes by subclassing UserString from the collections module
  • Decide when to use str or UserString to create custom string-like classes

Meanwhile, you’ll write a few examples that’ll help you decide whether to use str or UserString when you’re creating your custom string classes. Your choice will mostly depend on your specific use case.

To follow along with this tutorial, it’ll help if you’re familiar with Python’s built-in str class and its standard features. You’ll also need to know the basics of object-oriented programming and inheritance in Python.

Creating String-Like Classes in Python

The built-in str class allows you to create strings in Python. Strings are sequences of characters that you’ll use in many situations, especially when working with textual data. From time to time, the standard functionalities of Python’s str may be insufficient to fulfill your needs. So, you may want to create custom string-like classes that solve your specific problem.

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

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

You can also face situations in which you need to both extend and modify the standard functionality of strings at the same time.

In Python, you’ll commonly use one of the following techniques to create your string-like classes. You can inherit from the Python built-in str class directly or subclass UserString from collections.

One relevant feature of Python strings is immutability, which means that you can’t modify them in place. So, when selecting the appropriate technique to create your own custom string-like classes, you need to consider whether your desired features will affect immutability or not.

For example, if you need to modify the current behavior of existing string methods, then you’ll probably be okay subclassing str. In contrast, if you need to change how strings are created, then inheriting from str will demand advanced knowledge. You’ll have to override the .__new__() method. In this latter case, inheriting from UserString may make your life easier because you won’t have to touch .__new__().

In the upcoming sections, you’ll learn the pros and cons of each technique so that you can decide which is the best strategy to use for your specific problem.

Inheriting From Python’s Built-in str 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 str. This new feature is quite convenient when you need to create custom string-like classes.

By inheriting from str directly, you can extend and modify the standard behavior of this built-in class. You can also tweak the instantiation process of your custom string-like classes to perform transformations before new instances are ready.

Extending the String’s Standard Behavior

An example of requiring a custom string-like class is when you need to extend the standard Python strings with new behavior. For example, say that you need a string-like class that implements a new method to count the number of words in the underlying string.

Read the full article at »

[ 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 ]

September 28, 2022 at 07:30PM
Click here for more details...

The original post is available in Real Python by
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.