harmful str(bytes)

Antoine Pitrou solipsis at pitrou.net
Fri Oct 8 10:06:16 CEST 2010

On Thu, 07 Oct 2010 23:33:35 +0200
Hallvard B Furuseth <h.b.furuseth at usit.uio.no> wrote:
> The offender is bytes.__str__: str(b'foo') == "b'foo'".
> It's often not clear from looking at a piece of code whether
> some data is treated as strings or bytes, particularly when
> translating from old code.  Which means one cannot see from
> context if str(s) or "%s" % s will produce garbage.

This probably comes from overuse of str(s) and "%s". They can be useful
to produce human-readable messages, but you shouldn't have to use them
very often.

> I really wish bytes.__str__ would at least by default fail.

Actually, the implicit contract of __str__ is that it never fails, so
that everything can be printed out (for debugging purposes, etc.).



More information about the Python-list mailing list