You've been seeing things that somewhat conflict based on Python 2 vs 3. In Python 3, isinstance(foo, str)
is almost certainly what you want. bytes
is for raw binary data, which you probably can't include in an argument string like that.
The python 2 str
type stored raw binary data, usually a string in some specific encoding like utf8 or latin-1 or something; the unicode
type stored a more "abstract" representation of the characters that could then be encoded into whatever specific encoding. basestring
was a common ancestor for both of them so you could easily say "any kind of string".
In python 3, str
is the more "abstract" type, and bytes
is for raw binary data (like a string in a specific encoding, or whatever raw binary data you want to handle). You shouldn't use bytes
for anything that would otherwise be a string, so there's not a real reason to check if it's either str
or bytes
. If you absolutely need to, though, you can do something like isinstance(foo, (str, bytes))
.