ByteString
was added to give you a way to test for the "bytes-like type" that appears all over the 3.x docs without having to write (bytes, bytearray)
.
In fact, the docstring for it is just "This unifies bytes and bytearray."
There is no similar need to for Unicode strings, because str
is the only such type; there's nothing to unify it with.
You can click on the source link at the top of the docs, find ByteString
, and git blame
it right from the GitHub GUI to find the commit that added it. The checkin comment is:
Add ABC ByteString which unifies bytes and bytearray (but not memoryview).
There's no ABC for "PEP 3118 style buffer API objects" because there's no
way to recognize these in Python (apart from trying to use memoryview()
on them).
Note that array.array really should be registered as a MutableSequence
but that would require importing it whenever collections is imported.
There might be further discussion on b.p.o. or the python-dev or maybe python-ideas mailing list archives near 21 Nov 2007, if you really want to dig deeper. But I doubt there's much more of interest there, because there's really not much to discuss here.
Note that typing
actually does have a type for this, Text
, which is documented as:
Text
is an alias for str
. It is provided to supply a forward compatible path for Python 2 code: in Python 2, Text is an alias for unicode
.
Use Text
to indicate that a value must contain a unicode string in a manner that is compatible with both Python 2 and Python 3:
As the docs make clear, this wasn't added to unify multiple Unicode string types within the same language, but to unify Python 2 unicode
and Python 3 str
, at static type checking time.
At runtime, if you want this, you almost certainly want the actual str
or unicode
constructor, so you'd use something like six.text_type
.