https://docs.python.org/3/whatsnew/3.1.html
- Directories and zip archives containing a
__main__.py
file can now be executed directly by passing their name to the interpreter. The directory/zipfile is automatically inserted as the first entry insys.path
. - The
int()
type gained abit_length
method that returns the number of bits necessary to represent its argument in binary. - The fields in
format()
strings can now be automatically numbered. - The
string.maketrans()
function is deprecated and is replaced by new static methods,bytes.maketrans()
andbytearray.maketrans()
. This change solves the confusion around which types were supported by the string module. Now,str
,bytes
, andbytearray
each have their ownmaketrans
andtranslate
methods with intermediate translation tables of the appropriate type. - The syntax of the
with
statement now allows multiple context managers in a single statement. -
round(x, n)
now returns an integer ifx
is an integer. Previously it returned afloat
. - Python now uses David Gay's algorithm for finding the shortest floating point representation that doesn't change its value.
- Added a
collections.Counter
class to support convenient counting of unique items in a sequence or iterable. - Added a new module,
tkinter.ttk
for access to the Tk themed widget set. The basic idea ofttk
is to separate, to the extent possible, the code implementing a widget's behavior from the code implementing its appearance. - The
gzip.GzipFile
andbz2.BZ2File
classes now support the context management protocol. - The
decimal
module now supports methods for creating a decimal object from a binary float. - The
itertools
module grew two new functions. Theitertools.combinations_with_replacement()
function is one of four for generating combinatorics including permutations and Cartesian products. Theitertools.compress()
function mimics its namesake from APL. Also, the existingitertools.count()
function now has an optional step argument and can accept any type of counting sequence includingfractions.Fraction
anddecimal.Decimal
. -
collections.namedtuple()
now supports a keyword argument rename which lets invalid fieldnames be automatically converted to positional names in the form_0
,_1
, etc. This is useful when the field names are being created by an external source such as a CSV header, SQL field list, or user input. - The
re.sub()
,re.subn()
andre.split()
functions now accept aflags
parameter. - The
logging
module now implements a simplelogging.NullHandler
class for applications that are not using logging but are calling library code that does. Setting-up a null handler will suppress spurious warnings such as "No handlers could be found for logger foo". - The
runpy
module which supports the-m
command line switch now supports the execution of packages by looking for and executing a__main__
submodule when a package name is supplied. - The
pdb
module can now access and display source code loaded viazipimport
(or any other conformant PEP 302 loader). -
functools.partial
objects can now be pickled. - Add
pydoc
help topics for symbols so thathelp('@')
works as expected in the interactive environment. - The
unittest
module now supports skipping individual tests or classes of tests. And it supports marking a test as an expected failure, a test that is known to be broken, but shouldn't be counted as a failure on aTestResult
. Also, tests for exceptions have been built out to work with context managers using thewith
statement. In addition, several new assertion methods were added includingassertSetEqual()
,assertDictEqual()
,assertDictContainsSubset()
,assertListEqual()
,assertTupleEqual()
,assertSequenceEqual()
,assertRaisesRegexp()
,assertIsNone()
, andassertIsNotNone()
. - The
io
module has three new constants for theseek()
method:SEEK_SET
,SEEK_CUR
, andSEEK_END
. -
sys.version_info
tuple is now a named tuple. - The
nntplib
andimaplib
modules now support IPv6. - The
pickle
module has been adapted for better interoperability with Python 2.x when used with protocol 2 or lower. The reorganization of the standard library changed the formal reference for many objects. For example,__builtin__.set
in Python 2 is calledbuiltins.set
in Python 3. This change confounded efforts to share data between different versions of Python. But now when protocol 2 or lower is selected, the pickler will automatically use the old Python 2 names for both loading and dumping. This remapping is turned-on by default but can be disabled with thefix_imports
option. An unfortunate but unavoidable side-effect of this change is that protocol 2 pickles produced by Python 3.1 won't be readable with Python 3.0. The latest pickle protocol, protocol 3, should be used when migrating data between Python 3.x implementations, as it doesn't attempt to remain compatible with Python 2.x. - A new module,
importlib
was added. It provides a complete, portable, pure Python reference implementation of theimport
statement and its counterpart, the__import__()
function. It represents a substantial step forward in documenting and defining the actions that take place during imports.