Skip to content

Instantly share code, notes, and snippets.

@pfreixes
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfreixes/4aea1c5642c85b80efe8 to your computer and use it in GitHub Desktop.
Save pfreixes/4aea1c5642c85b80efe8 to your computer and use it in GitHub Desktop.
A not negligible difference between Python 2 and Python 3
"""Programs not ported to Python 3 that they are using specific
operations with strings could raise undesirable bugs"""
>>> title = "Open binary file with python 3"
>>> fd = open("/tmp/wut.png", "rb")
>>> b = fd.read()
>>> type(b)
<class 'bytes'>
>>> b[0] == "\x89"
False
>>> title = "Open binary file with python 2"
>>> fd = open("/tmp/wut.png", "rb")
>>> b = fd.read()
>>> type(b)
<type 'str'>
>>> b[0] == "\x89"
True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment