Python 3 Gotchas




Pay Notebook Creator: Roy Hyunjin Han0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0
In [1]:
hasattr('xyz', '__iter__')  # False in Python 2
Out[1]:
True
In [2]:
from pytest import raises
with raises(NameError):
    xrange(10)
with raises(NameError):
    basestring
In [3]:
bytes
Out[3]:
bytes
In [ ]:
# TypeError: string argument expected, got 'bytes'
import io
io.BytesIO  # Use BytesIO instead of StringIO
In [5]:
from pytest import raises
with raises(ImportError):
    import cStringIO
with raises(ImportError):
    import StringIO
In [11]:
from six import StringIO
from pytest import raises
with raises(AttributeError):
    StringIO().reset
StringIO().seek(0)
Out[11]:
0