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