When having a look at the types str
and bytes
in Python, it turns out they are very similiar. The only differences wrt. their attributes are:
>>> set(dir(bytes)) - set(dir(str))
{'hex', 'fromhex', 'decode'}
>>> set(dir(str)) - set(dir(bytes))
{'isidentifier', 'encode', 'isdecimal', 'isnumeric', 'casefold', 'format', 'isprintable', 'format_map'}
Checking the Python documentation, I figured that these differences should not be relevant for their relation to the abstract base class collections.abc.ByteString
. However, bytes
is regarded a subclass while str
is not:
>>> issubclass(bytes, collections.abc.ByteString)
True
>>> issubclass(str, collections.abc.ByteString)
False
While the observed behaviour is useful to discern these types, I do not understand why Python behaves that way. In my understanding of Python's duck typing concept, both str
and bytes
should be regarded as subclasses, as long as they bring the relevant attributes.