arrays - Python string "b" prefix (byte literals) -
i looking through unit testing code , found this:
self.assertin(b'hello', res.body)
i know means bytes
in python 3 returns byte array, found here. believe code written python 3.3 , trying figure out how works in other versions (in case 2.7) related question found had poorly-written accepted answer contradictory comments confused me.
questions:
- in versions of python
b'mystring'
"work"? - how behave in python 2.x?
- how behave in python 3.x?
- does have byte literal change?
this described in document linked.
- in versions of python
b'mystring'
"work"?: 2.6+. - how behave in python 2.x? it creates
bytes
literal—which exact same thingstr
literal in 2.x. - how behave in python 3.x? it creates
bytes
literal—which not same thingstr
literal in 3.x. - does have byte literal change? yes. that's whole point; lets write "future compatible" code—or code works in both 2.6+ , 3.0+ without
2to3
.
quoting first paragraph in section linked:
for future compatibility, python 2.6 adds
bytes
synonymstr
type, , supports b'' notation.
note that, says few lines down, python 2.x bytes
/str
not same type python 3.x bytes
: "most notably, constructor different". bytes literals same, except in edge case you're putting unicode characters bytes literal (which has no defined meaning in 2.x, arbitrary may happen you'd hoped, while in 3.x it's guaranteed syntaxerror
).
Comments
Post a Comment