Python 3.1 有什麼新功能¶
- 作者:
Raymond Hettinger
本文介紹了 Python 3.1 與 3.0 相比的新功能。Python 3.1 已於 2009 年 6 月 27 日發布。
PEP 372:有序字典¶
Regular Python dictionaries iterate over key/value pairs in arbitrary order.Over the years, a number of authors have written alternative implementationsthat remember the order that the keys were originally inserted. Based onthe experiences from those implementations, a newcollections.OrderedDict
class has been introduced.
The OrderedDict API is substantially the same as regular dictionariesbut will iterate over keys and values in a guaranteed order depending onwhen a key was first inserted. If a new entry overwrites an existing entry,the original insertion position is left unchanged. Deleting an entry andreinserting it will move it to the end.
The standard library now supports use of ordered dictionaries in severalmodules. Theconfigparser
module uses them by default. This letsconfiguration files be read, modified, and then written back in their originalorder. The_asdict() method forcollections.namedtuple()
nowreturns an ordered dictionary with the values appearing in the same order asthe underlying tuple indices. Thejson
module is being built-out withanobject_pairs_hook to allow OrderedDicts to be built by the decoder.Support was also added for third-party tools likePyYAML.
也參考
- PEP 372 - 有序字典
PEP written by Armin Ronacher and Raymond Hettinger. Implementationwritten by Raymond Hettinger.
Since an ordered dictionary remembers its insertion order, it can be usedin conjunction with sorting to make a sorted dictionary:
>>># regular unsorted dictionary>>>d={'banana':3,'apple':4,'pear':1,'orange':2}>>># dictionary sorted by key>>>OrderedDict(sorted(d.items(),key=lambdat:t[0]))OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])>>># dictionary sorted by value>>>OrderedDict(sorted(d.items(),key=lambdat:t[1]))OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])>>># dictionary sorted by length of the key string>>>OrderedDict(sorted(d.items(),key=lambdat:len(t[0])))OrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)])
The new sorted dictionaries maintain their sort order when entriesare deleted. But when new keys are added, the keys are appendedto the end and the sort is not maintained.
PEP 378: Format Specifier for Thousands Separator¶
The built-informat()
function and thestr.format()
method usea mini-language that now includes a simple, non-locale aware way to formata number with a thousands separator. That provides a way to humanize aprogram's output, improving its professional appearance and readability:
>>>format(1234567,',d')'1,234,567'>>>format(1234567.89,',.2f')'1,234,567.89'>>>format(12345.6+8901234.12j,',f')'12,345.600000+8,901,234.120000j'>>>format(Decimal('1234567.89'),',f')'1,234,567.89'
The supported types areint
,float
,complex
anddecimal.Decimal
.
Discussions are underway about how to specify alternative separatorslike dots, spaces, apostrophes, or underscores. Locale-aware applicationsshould use the existingn format specifier which already has some supportfor thousands separators.
也參考
- PEP 378 - Format Specifier for Thousands Separator
PEP written by Raymond Hettinger and implemented by Eric Smith andMark Dickinson.
其他語言更動¶
Some smaller changes made to the core Python language are:
Directories and zip archives containing a
__main__.py
file can now be executed directly by passing their name to theinterpreter. The directory/zipfile is automatically inserted as thefirst entry in sys.path. (Suggestion and initial patch by Andy Chu;revised patch by Phillip J. Eby and Nick Coghlan;bpo-1739468.)The
int()
type gained abit_length
method that returns thenumber of bits necessary to represent its argument in binary:>>>n=37>>>bin(37)'0b100101'>>>n.bit_length()6>>>n=2**123-1>>>n.bit_length()123>>>(n+1).bit_length()124
(由 Fredrik Johansson、Victor Stinner、Raymond Hettinger 和 Mark Dickinson 貢獻;bpo-3439。)
The fields in
format()
strings can now be automaticallynumbered:>>>'Sir{} of{}'.format('Gallahad','Camelot')'Sir Gallahad of Camelot'
Formerly, the string would have required numbered fields such as:
'Sir{0}of{1}'
.(由 Eric Smith 貢獻;bpo-5237。)
The
string.maketrans()
function is deprecated and is replaced by newstatic methods,bytes.maketrans()
andbytearray.maketrans()
.This change solves the confusion around which types were supported by thestring
module. Now,str
,bytes
, andbytearray
each have their ownmaketrans andtranslatemethods with intermediate translation tables of the appropriate type.(由 Georg Brandl 貢獻;bpo-5675。)
The syntax of the
with
statement now allows multiple contextmanagers in a single statement:>>>withopen('mylog.txt')asinfile,open('a.out','w')asoutfile:...forlineininfile:...if'<critical>'inline:...outfile.write(line)
With the new syntax, the
contextlib.nested()
function is no longerneeded and is now deprecated.(由 Georg Brandl 和 Mattias Brändström 貢獻;appspot 問題 53094。)
round(x,n)
now returns an integer ifx is an integer.Previously it returned a float:>>>round(1123,-2)1100
(由 Mark Dickinson 貢獻;bpo-4707。)
Python now uses David Gay's algorithm for finding the shortest floating-pointrepresentation that doesn't change its value. This should helpmitigate some of the confusion surrounding binary floating-pointnumbers.
The significance is easily seen with a number like
1.1
which does nothave an exact equivalent in binary floating point. Since there is no exactequivalent, an expression likefloat('1.1')
evaluates to the nearestrepresentable value which is0x1.199999999999ap+0
in hex or1.100000000000000088817841970012523233890533447265625
in decimal. Thatnearest value was and still is used in subsequent floating-pointcalculations.What is new is how the number gets displayed. Formerly, Python used asimple approach. The value of
repr(1.1)
was computed asformat(1.1,'.17g')
which evaluated to'1.1000000000000001'
. The advantage ofusing 17 digits was that it relied on IEEE-754 guarantees to assure thateval(repr(1.1))
would round-trip exactly to its original value. Thedisadvantage is that many people found the output to be confusing (mistakingintrinsic limitations of binary floating-point representation as being aproblem with Python itself).The new algorithm for
repr(1.1)
is smarter and returns'1.1'
.Effectively, it searches all equivalent string representations (ones thatget stored with the same underlying float value) and returns the shortestrepresentation.The new algorithm tends to emit cleaner representations when possible, butit does not change the underlying values. So, it is still the case that
1.1+2.2!=3.3
even though the representations may suggest otherwise.The new algorithm depends on certain features in the underlying floating-pointimplementation. If the required features are not found, the oldalgorithm will continue to be used. Also, the text pickle protocolsassure cross-platform portability by using the old algorithm.
(由 Eric Smith 和 Mark Dickinson 貢獻;bpo-1580)
New, Improved, and Deprecated Modules¶
Added a
collections.Counter
class to support convenientcounting of unique items in a sequence or iterable:>>>Counter(['red','blue','red','green','blue','blue'])Counter({'blue': 3, 'red': 2, 'green': 1})
(由 Raymond Hettinger 貢獻;bpo-1696199。)
Added a new module,
tkinter.ttk
for access to the Tk themed widget set.The basic idea of ttk is to separate, to the extent possible, the codeimplementing a widget's behavior from the code implementing its appearance.(由 Guilherme Polo 貢獻;bpo-2983。)
The
gzip.GzipFile
andbz2.BZ2File
classes now supportthe context management protocol:>>># Automatically close file after writing>>>withgzip.GzipFile(filename,"wb")asf:...f.write(b"xxx")
(由 Antoine Pitrou 貢獻。)
The
decimal
module now supports methods for creating adecimal object from a binaryfloat
. The conversion isexact but can sometimes be surprising:>>>Decimal.from_float(1.1)Decimal('1.100000000000000088817841970012523233890533447265625')
The long decimal result shows the actual binary fraction beingstored for1.1. The fraction has many digits because1.1 cannotbe exactly represented in binary.
(由 Raymond Hettinger 和 Mark Dickinso 貢獻。)
The
itertools
module grew two new functions. Theitertools.combinations_with_replacement()
function is one offour for generating combinatorics including permutations and Cartesianproducts. Theitertools.compress()
function mimics its namesakefrom APL. Also, the existingitertools.count()
function now hasan optionalstep argument and can accept any type of countingsequence includingfractions.Fraction
anddecimal.Decimal
:>>>[p+qforp,qincombinations_with_replacement('LOVE',2)]['LL', 'LO', 'LV', 'LE', 'OO', 'OV', 'OE', 'VV', 'VE', 'EE']>>>list(compress(data=range(10),selectors=[0,0,1,1,0,1,0,1,0,0]))[2, 3, 5, 7]>>>c=count(start=Fraction(1,2),step=Fraction(1,6))>>>[next(c),next(c),next(c),next(c)][Fraction(1, 2), Fraction(2, 3), Fraction(5, 6), Fraction(1, 1)]
(由 Raymond Hettinger 貢獻。)
collections.namedtuple()
now supports a keyword argumentrename which lets invalid fieldnames be automatically converted topositional names in the form _0, _1, etc. This is useful whenthe field names are being created by an external source such as aCSV header, SQL field list, or user input:>>>query=input()SELECT region, dept, count(*) FROM main GROUPBY region, dept>>>cursor.execute(query)>>>query_fields=[desc[0]fordescincursor.description]>>>UserQuery=namedtuple('UserQuery',query_fields,rename=True)>>>pprint.pprint([UserQuery(*row)forrowincursor])[UserQuery(region='South', dept='Shipping', _2=185), UserQuery(region='North', dept='Accounting', _2=37), UserQuery(region='West', dept='Sales', _2=419)]
(由 Raymond Hettinger 貢獻;bpo-1818。)
The
re.sub()
,re.subn()
andre.split()
functions nowaccept a flags parameter.(由 Gregory Smith 貢獻。)
The
logging
module now implements a simplelogging.NullHandler
class for applications that are not using logging but are callinglibrary code that does. Setting-up a null handler will suppressspurious warnings such as "No handlers could be found for logger foo":>>>h=logging.NullHandler()>>>logging.getLogger("foo").addHandler(h)
(由 Vinay Sajip 貢獻;bpo-4384)。
The
runpy
module which supports the-m
command line switchnow supports the execution of packages by looking for and executinga__main__
submodule when a package name is supplied.(由 Andi Vajda 貢獻;bpo-4195。)
The
pdb
module can now access and display source code loaded viazipimport
(or any other conformantPEP 302 loader).(由 Alexander Belopolsky 貢獻;bpo-4201。)
functools.partial
objects can now be pickled.
(Suggested by Antoine Pitrou and Jesse Noller. Implemented byJack Diederich;bpo-5228.)
Add
pydoc
help topics for symbols so thathelp('@')
works as expected in the interactive environment.(由 David Laban 貢獻;bpo-4739。)
The
unittest
module now supports skipping individual tests or classesof tests. And it supports marking a test as an expected failure, a test thatis known to be broken, but shouldn't be counted as a failure on aTestResult:classTestGizmo(unittest.TestCase):@unittest.skipUnless(sys.platform.startswith("win"),"requires Windows")deftest_gizmo_on_windows(self):...@unittest.expectedFailuredeftest_gimzo_without_required_library(self):...
Also, tests for exceptions have been builtout to work with context managersusing the
with
statement:deftest_division_by_zero(self):withself.assertRaises(ZeroDivisionError):x/0
In addition, several new assertion methods were added including
assertSetEqual()
,assertDictEqual()
,assertDictContainsSubset()
,assertListEqual()
,assertTupleEqual()
,assertSequenceEqual()
,assertRaisesRegexp()
,assertIsNone()
,andassertIsNotNone()
.(由 Benjamin Peterson 和 Antoine Pitrou 貢獻。)
The
io
module has three new constants for theseek()
method:SEEK_SET
,SEEK_CUR
, andSEEK_END
.The
sys.version_info
tuple is now a named tuple:>>>sys.version_infosys.version_info(major=3, minor=1, micro=0, releaselevel='alpha', serial=2)
(由 Ross Light 貢獻;bpo-4285。)
The
nntplib
andimaplib
modules now support IPv6.The
pickle
module has been adapted for better interoperability withPython 2.x when used with protocol 2 or lower. The reorganization of thestandard library changed the formal reference for many objects. Forexample,__builtin__.set
in Python 2 is calledbuiltins.set
in Python3. This change confounded efforts to share data between different versions ofPython. But now when protocol 2 or lower is selected, the pickler willautomatically use the old Python 2 names for both loading and dumping. Thisremapping is turned-on by default but can be disabled with thefix_importsoption:>>>s={1,2,3}>>>pickle.dumps(s,protocol=0)b'c__builtin__\nset\np0\n((lp1\nL1L\naL2L\naL3L\natp2\nRp3\n.'>>>pickle.dumps(s,protocol=0,fix_imports=False)b'cbuiltins\nset\np0\n((lp1\nL1L\naL2L\naL3L\natp2\nRp3\n.'
An unfortunate but unavoidable side-effect of this change is that protocol 2pickles produced by Python 3.1 won't be readable with Python 3.0. The latestpickle protocol, protocol 3, should be used when migrating data betweenPython 3.x implementations, as it doesn't attempt to remain compatible withPython 2.x.
(由 Alexandre Vassalotti 和 Antoine Pitrou 貢獻,bpo-6137。)
A new module,
importlib
was added. It provides a complete, portable,pure Python reference implementation of theimport
statement and itscounterpart, the__import__()
function. It represents a substantialstep forward in documenting and defining the actions that take place duringimports.(由 Brett Cannon 貢獻。)
最佳化¶
Major performance enhancements have been added:
The new I/O library (as defined inPEP 3116) was mostly written inPython and quickly proved to be a problematic bottleneck in Python 3.0.In Python 3.1, the I/O library has been entirely rewritten in C and is2 to 20 times faster depending on the task at hand. The pure Pythonversion is still available for experimentation purposes throughthe
_pyio
module.(由 Amaury Forgeot d'Arc 和 Antoine Pitrou 貢獻。)
Added a heuristic so that tuples and dicts containing only untrackable objectsare not tracked by the garbage collector. This can reduce the size ofcollections and therefore the garbage collection overhead on long-runningprograms, depending on their particular use of datatypes.
(由 Antoine Pitrou 貢獻,bpo-4688。)
Enabling a configure option named
--with-computed-gotos
on compilers that support it (notably: gcc, SunPro, icc), the bytecodeevaluation loop is compiled with a new dispatch mechanism which givesspeedups of up to 20%, depending on the system, the compiler, andthe benchmark.(由 Antoine Pitrou 和其他一些參與者共同貢獻,bpo-4753)。
The decoding of UTF-8, UTF-16 and LATIN-1 is now two to four timesfaster.
(由 Antoine Pitrou 和 Amaury Forgeot d'Arc 貢獻,bpo-4868。)
The
json
module now has a C extension to substantially improveits performance. In addition, the API was modified so that json worksonly withstr
, not withbytes
. That change makes themodule closely match theJSON specificationwhich is defined in terms of Unicode.(由 Bob Ippolito 貢獻,由 Antoine Pitrou 和 Benjamin Peterson 轉換為 Py3.1;bpo-4136。)
Unpickling now interns the attribute names of pickled objects. This savesmemory and allows pickles to be smaller.
(由 Jake McGuire 和 Antoine Pitrou 貢獻;bpo-5084。)
IDLE¶
IDLE's format menu now provides an option to strip trailing whitespacefrom a source file.
(由 Roger D. Serwy 貢獻;bpo-5150。)
建置和 C API 變更¶
Python 建置程序和 C API 的變更包括:
Integers are now stored internally either in base
2**15
or in base2**30
, the base being determined at build time. Previously, theywere always stored in base2**15
. Using base2**30
givessignificant performance improvements on 64-bit machines, butbenchmark results on 32-bit machines have been mixed. Therefore,the default is to use base2**30
on 64-bit machines and base2**15
on 32-bit machines; on Unix, there's a new configure option--enable-big-digits
that can be used to override this default.Apart from the performance improvements this change should be invisible toend users, with one exception: for testing and debugging purposes there's anew
sys.int_info
that provides information about theinternal format, giving the number of bits per digit and the size in bytesof the C type used to store each digit:>>>importsys>>>sys.int_infosys.int_info(bits_per_digit=30, sizeof_digit=4)
(由 Mark Dickinson 貢獻;bpo-4258。)
The
PyLong_AsUnsignedLongLong()
function now handles a negativepylong by raisingOverflowError
instead ofTypeError
.(由 Mark Dickinson 和 Lisandro Dalcrin 貢獻;bpo-5175。)
已棄用
PyNumber_Int()
。請改用PyNumber_Long()
。(由 Mark Dickinson 貢獻;bpo-4910。)
Added a new
PyOS_string_to_double()
function to replace thedeprecated functionsPyOS_ascii_strtod()
andPyOS_ascii_atof()
.(由 Mark Dickinson 貢獻;bpo-5914。)
Added
PyCapsule
as a replacement for thePyCObject
API.The principal difference is that the new type has a well defined interfacefor passing typing safety information and a less complicated signaturefor calling a destructor. The old type had a problematic API and is nowdeprecated.(由 Larry Hastings 貢獻;bpo-5630。)
移植至 Python 3.1¶
This section lists previously described changes and other bugfixesthat may require changes to your code:
The new floating-point string representations can break existing doctests.For example:
defe():'''計算自然對數的底。 >>> e() 2.7182818284590451 '''returnsum(1/math.factorial(x)forxinreversed(range(30)))doctest.testmod()**********************************************************************Failedexample:e()Expected:2.7182818284590451Got:2.718281828459045**********************************************************************
The automatic name remapping in the pickle module for protocol 2 or lower canmake Python 3.1 pickles unreadable in Python 3.0. One solution is to useprotocol 3. Another solution is to set thefix_imports option to
False
.See the discussion above for more details.