Python 2-to-3 Cheat Sheet
A short reference of new Python 3 features.
Python 3.4
- PEP 446: Newly created file descriptors non-inheritable by default.
min(..., default=...)
andmax(..., default=...)
.asyncio
. Status: Provisional.enum
.pathlib
. Status: Provisional.
Python 3.3
yield from
statement:- Receive sent and thrown values from the calling scope.
- Return value to the outer generator.
venv
module andpyvenv
script.raise exc from None
to suppress exception context.collections.ChainMap
.
Python 3.2
optparse
argparse
.str.format_map()
.@functools.lru_cache(maxsize=...)
.@functools.total_ordering
.collection.OrderedDict.move_to_end(key, last=True)
.threading.Barrier
.io.BytesIO.getbuffer()
returns an editable view of the internal data.@reprlib.recursive_repr()
for recursive structure.with subprocess.Popen(...)
.with tempfile.TemporaryDirectory()
.
Python 3.1
collections.OrderedDict
, which is also used byconfigparser
,collections.namedtuple
,json
, etc.collections.Counter
.format(1234567.89, ',.2f') == '1,234,567.89'
.n.bit_length()
.- Multiple context managers:
collections.namedtuple(..., rename=False)
.logging.NullHandler
.importlib
.
Python 3.0
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
.exec()
is a function.dict.keys()
,dict.items()
, anddict.values()
return views.dict.iterkeys()
dict.iteritems()
dict.itervalues()
map()
andfilter()
return iterators.map()
stops when the shortest iterator is exhausted.xrange()
range()
.zip()
returns an iterator.- You cannot make comparison of
None < None
,1 < ''
, etc. list.sort()
andsorted()
:cmp
key
.object.__cmp__()
.long
int
.1 / 2 == 0.5
and4 // 2 == 1
.sys.maxint
sys.maxsize
(since “maxint” makes not sense anymore).- Octal literal:
0o720
. No0720
anymore.oct(16) == '0o20'
. - Binary literal:
0b101
.bin(8) == '0b1000'
. str
is a sequence of Unicode code points.str(byte_string, encoding=...)
vsbytes(text_string, encoding=...)
.'Unicode string literal'
vsb'byte string literal'
.(since there is notbasestring
unicode
andstr
now).- Remember to pass
encoding
toopen(..., encoding=None, ...)
when opening in text mode. sys.stdin
et al. are now Unicode-only text files (io.TextIOBase
). Useio.TextIOBase.bufer
for byte data.- File names and paths are now Unicode strings, but:
- Some APIs accept byte strings, too.
- Some APIs return byte strings when passing in byte strings, too.
os.environ
,sys.argv
, etc. might not be interpretable using the current encoding.StringIO
cStringIO
io.StringIO
andio.BytesIO
.- PEP 3107: Argument and return value annotations.
- PEP 3102: Keyword-only arguments.
- PEP 3104:
nonlocal
, lexical scoping. - PEP 3132:
a, b, *rest = any_iterable
or*rest, c = stuff
. - PEP 0274: Dictionary comprehensions
{k : v for k, v in stuff}
and set comprehensions{e for e in stuff}
. - Set literal
{1, 2}
.{}
is an empty dictionary. - PEP 3109 and PEP 3134: Exception chaining:
True
,False
, andNone
are reserved words.except exc as var
.object.__metaclass__
class C(metaclass=M)
.- Ellipsis
...
is a built-in constant. - Classic classes are gone.
- All import forms not starting with
.
are interpreted as absolute imports. - PEP 3129: Class decorators.