x-yuri
x-yuri

Reputation: 18953

What's the relationship between str and bytes in python?

Consider the following typescript:

>>> s = 'a'
>>> isinstance(s, bytes)
True
>>> isinstance(s, str)
True
>>> isinstance(s, unicode)
False
>>> isinstance(s.decode('utf-8'), unicode)
True

How come s is both a str and a bytes? Is one of those a descendant of the other one?

How did I run into it? I was trying to find description of decode method in the docs. I couldn't find it for str, but was able for bytes.

Upvotes: 1

Views: 68

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599798

You are looking at the wrong documentation.

This equivalence is only true in Python 2.7. There, bytes was introduced as an alias to str in order to ease migration to Python 3.

In Python 3, str is what was previously called unicode, bytes is the type that was previously called str.

The documentation for str.decode for Python 2 is here.

Upvotes: 4

Related Questions