Implementing UserString
Just like UserDict
and UserList
, UserString
is a string wrapper that allows easier subclassing of strings due to providing the underlying string as an attribute. The preferred way to do this is to subclass string
directly; this class is provided mainly due to backwards-compatibility or simple cases where subclassing string
is overkill for functionality.
While all string methods are available, such as UserDict
and UserList
, UserString
adds the data
attribute for easy access to the underlying string object. The contents of UserString
are initially set to a copy of some type of sequence; the sequence can be bytes, a string, another UserString
or subclass, or any other sequence object that can be converted to a string.
How to do it...
userstring_import.py
is simple in that it shows how to create a method to append a sequence to a string, much like adding more items to a list:
>>> from collections import UserString >>> class AppendString(UserString...