Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am looking through some unit testing code and I found this:

self.assertIn(b'Hello', res.body)

I know that this means bytes in python 3 which returns a byte array, as i found here. I believe that the code was written for python 3.3 and am trying to figure out how it works in other versions (in my case 2.7) The related question that I found had a poorly written accepted answer with contradictory comments that confused me.

Questions:

  • In what versions of python does b'myString' "work"?
  • How does it behave in python 2.x?
  • How does it behave in python 3.x?
  • Does that have something to do with the byte literal change?
share|improve this question
 
What's contradictory about the comments to that answer? It's just the answerer and another user helping each other out looking for reference links to back up the very simple answer. (Section 2.4.1 of the reference docs had a bug in 2.6, but it was fixed; see here.) –  abarnert Jul 17 '13 at 21:37
 
And what part of the answer is unclear? It specifically says it's a bytes literal in 3.x, it's not legal at all in 2.5 and older, and it's equivalent to a plain string in 2.6+ for future compatibility. The only thing remotely confusable is whether 2.6+ includes 3.x (it doesn't; that was already covered in the first part). –  abarnert Jul 17 '13 at 21:38
 
Finally, bytes does not return a bytearray in 3.x. Those are two separate but related types, similar to frozenset vs. set or tuple vs. list. –  abarnert Jul 17 '13 at 21:40
 
The answer uses the word "it" a total of two times. One of them is inside a parenthetical aside that you can completely ignore. The other, I can't imagine what "it" could possibly refer to besides either "bytes literal" or "this prefix", and it makes perfect sense either way. –  abarnert Jul 17 '13 at 22:12
1  
More importantly, if you think an answer is confusing, comment on the answer; don't create a new question asking for comments on the answer to another question. –  abarnert Jul 17 '13 at 22:12
add comment

1 Answer

This is all described in the document you linked.

  • In what versions of python does b'myString' "work"?: 2.6+.
  • How does it behave in python 2.x? It creates a bytes literal—which is the exact same thing as a str literal in 2.x.
  • How does it behave in python 3.x? It creates a bytes literal—which is not the same thing as a str literal in 3.x.
  • Does that have something to do with the byte literal change? Yes. That's the whole point; it lets you write "future compatible" code—or code that works in both 2.6+ and 3.0+ without 2to3.

Quoting from the first paragraph in the section you linked:

For future compatibility, Python 2.6 adds bytes as a synonym for the str type, and it also supports the b'' notation.

Note that, as it says a few lines down, Python 2.x bytes/str is not exactly the same type as Python 3.x bytes: "most notably, the constructor is completely different". But bytes literals are the same, except in the edge case where you're putting Unicode characters into a bytes literal (which has no defined meaning in 2.x, but does something arbitrary that may sometimes happen to be what you'd hoped, while in 3.x it's a guaranteed SyntaxError).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.