I have code that expects str
but will handle the case of being passed bytes
in the following way:
if isinstance(data, bytes):
data
You can use:
isinstance(data, (bytes, bytearray))
Due to the different base class is used here.
>>> bytes.__base__
>>> bytearray.__base__
To check bytes
>>> by = bytes()
>>> isinstance(by, basestring)
True
However,
>>> buf = bytearray()
>>> isinstance(buf, basestring)
False
The above codes are test under python 2.7
Unfortunately, under python 3.4, they are same....
>>> bytes.__base__
>>> bytearray.__base__