2010-09-01: remove mention of built-ins returning iterators]
A question on Stack Overflow about what is exclusive to Python 3 came up and I realized that there is no clear list of big changes that you cannot access in Python 2.7 through a
__future__
import. So I figured I would go through the What's New docs for Python 3.0, 3.1, and 3.2a1 (although the What's New doc has not been written yet) and see what has (not) been backported of significance.If something is available in Python 2.6 without a
__future__
import I will not list it here (e.g., new octal literals, bytes literal, and str.format
). I also don't touch the C API. Otherwise stuff that is crossed out has been backported in Python 2.7 or is in Python 2.6 with a __future__
import. Everything else you have to make the switch to Python 3 to get the feature.- Python 3.0
print as a functionfrom __future__ import print_function
since Python 2.6.Views and iterators instead of listsfuture_builtins
since Python 2.6 contains versions of built-ins that match Python 3 semantics, including returning iterators instead of lists- Dicts gained equivalent view methods (e.g.,
dict.viewkeys()
) in Python 2.7 - Ordering comparisons
- Int/long unification
- Unicode/bytes clarification
from __future__ import unicode_literals
helps since Python 2.6- ... but only if you bother to rid your code of all references to
str
and move tounicode
andbytes
- Standard library changes in regards to bytes/strings in Python 3 only
Dict comprehensions- Python 2.7
- Link is to a withdrawn PEP, but the general idea holds
Set comprehensions- Python 2.7
Set literals- Python 2.7
- Removal of old syntax
- exec as a function
PEP 352: Required Superclass for Exceptions- PEP 3102: keyword-only arguments
- PEP 3104: nonlocal
- PEP 3107: function annotations
- PEP 3108: Standard library reorganization
- PEP 3115: Metaclasses
- PEP 3132: extended iterable unpacking
- PEP 3134: Exception Chaining and Embedded Tracebacks
- PEP 3135: new
super
PEP 3137:memoryview
- Python 2.7
- Python 3.1
PEP 372: ordered dictionary to collections- Python 2.7
PEP 378:Format Specifier for Thousands Separator- Python 2.7
Fields in format() can be auto-numbered- E.g.,
"{} {}".format(2, 5) == "2 5"
- Might seem minor, but damn does it make a difference!
- Added in Python 2.7
contextlib.nested no longer needed- Another minor but handy feature
- Python 2.7
Floats print their shortest representation- Python 2.7
tkinter.ttk- Added as
ttk
in Python 2.7 importlib
- Single function,
importlib.import_module
, in Python 2.7 sysconfig
- Python 2.7
io
rewritten in C- Python 2.7
- --with-computed-gotos
- Python 3.2a1
- GIL heavily reworked to perform better
Bazillions of improvements to unittest- Available externally as unittest2
- Updated in Python 2.7
argparse
- Python 2.7
PEP 391: dictionary-based configuration forlogging
- Python 2.7
- PEP 3147: PYC repository directories
- ... bunch of stuff I probably overlooked from Misc/NEWS
So what does the list tell us? First, a ton of syntactic cleanup only appears in Python 3, which is not surprising. Second, there are still plenty of reasons, both from a development perspective but also a performance one, to look forward to moving over to Python 3 when you can.