Whats New In Python 3.1
PEPs
- Added
OrderedDictin collections module- Built-informat(): format(1234567.89, ',.2f') => '1,234,567.89'
Other Language Changes
- Directories and zip archives containing a
__main__.pyfile can be executed by passing their names to the interpreter. bit_length():n = 2**123-1; n.bit_length()=>123'Sir {} of {}'.format('Foo', 'Bar')'- string.maketrans() was replaced by
bytes.maketrans() - with statement:
with open('a.txt') as infile, open('b.txt') as outfile: - round(x, n) now returns integer if x is an integer
- repr(1.1) now is '1.1', previously, it's '1.1000000000000001'
New, Improved, and Deprecated Modules
- Added collections.
Counterclass to support counting of unique items in a sequence. - Added a new module
tkinter.ttkfor access to the Tk themed widget set. - The
gzip.GzipFileandbz2.BZ2Fileclasses now support the context manager protocol (thewith..asstatement) - The
decimalmodule now supports methods for creating a decimal fromfloat:Decimal.from_float(1.1) - The
itertoolsmodule grew two new functions. Theitertools.combinations_with_replacement()for generating combinatorics including permutations and Cartesian products. Theitertools.compress()mimics namesake from APL. - Added the
renameargument forcollections.namedtuple() - The
re.sub(), re.subn()andre.split()now accept a flags parameter. - The
loggingmodule now implements a simplelogging.NullHandlerclass. - The
runnywhich supports the-mswitch now supports the execution the__main__when a package name is supplied. - The
pdbnow supportzipimportcodes functools.partialobjects can now be pickled.- The
unit testmodule now supports skipping individual tests or classes of tests. And it supports marking a test as a expected failure.@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")and@unittest.expectedFailure - Also, tests for exceptions have been builtout to use
withstatement. More assertion methods were added likeassertTupleEqual(), assertSequenceEqual(), etc. - The
iomodule has three new constants for theseek()method.SEEK_SET, etc - A new module
importlibwas added. It provides a complete, portable, pure Python reference implementation of theimportand__import__().
Optimizations
Major performance enhancements have been added:
- The I/O library has been entirely rewritten in C and is 2 to 20 times faster than it in Python 3.0 (The python version is still available at
_pyiofor experimentation purposes. - Added a heuristic so that tuples and dicts containing only untrackable objects are not tracked by the garbage collector.
- The decoding of UTF-8, UTF-16, and LATIN-1 is now two to four times faster.
- The
jsonmodule now has a C extension to substantially improve its performance. - Unpickling now interns the attribute names of pickled objects. This save memory and allow pickles to be smaller.