新聞中心
python 3.1 有什么新變化
作者

成都創(chuàng)新互聯(lián)公司是一家專注于做網(wǎng)站、成都網(wǎng)站制作與策劃設(shè)計(jì),遂川網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:遂川等地區(qū)。遂川做網(wǎng)站價(jià)格咨詢:028-86922220
Raymond Hettinger(譯者:wh2099 at outlook dot com)
本文介紹了 Python 3.1 相比 3.0 的新特性。 Python 3.1 發(fā)布于 2009 年 6 月 27 日。
PEP 372: 有序字典
常規(guī)的 Python 字典會(huì)以任意順序迭代鍵/值對。 多年以來,有好幾位作者編寫了可以記住鍵的初始插入順序的替代實(shí)現(xiàn)。 基于這些實(shí)現(xiàn)的經(jīng)驗(yàn),現(xiàn)在引入了新的 collections.OrderedDict 類。
The OrderedDict API is substantially the same as regular dictionaries but will iterate over keys and values in a guaranteed order depending on when a key was first inserted. If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end.
The standard library now supports use of ordered dictionaries in several modules. The configparser module uses them by default. This lets configuration files be read, modified, and then written back in their original order. The _asdict() method for collections.namedtuple() now returns an ordered dictionary with the values appearing in the same order as the underlying tuple indices. The json module is being built-out with an object_pairs_hook to allow OrderedDicts to be built by the decoder. Support was also added for third-party tools like PyYAML.
參見
PEP 372 - 有序字典
PEP 由 Armin Ronacher 和 Raymond Hettinger 撰寫,由 Raymond Hettinger 實(shí)現(xiàn)。
PEP 378: 千位分隔符的格式說明符
The built-in format() function and the str.format() method use a mini-language that now includes a simple, non-locale aware way to format a number with a thousands separator. That provides a way to humanize a program’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'
支持的類型有 int, float, complex 和 decimal.Decimal。
Discussions are underway about how to specify alternative separators like dots, spaces, apostrophes, or underscores. Locale-aware applications should use the existing n format specifier which already has some support for thousands separators.
參見
PEP 378 - 千位分隔符的格式說明符
PEP 由 Raymond Hettinger 撰寫,并由 Eric Smith 和 Mark Dickinson 實(shí)現(xiàn)
其他語言特性修改
對Python 語言核心進(jìn)行的小改動(dòng):
Directories and zip archives containing a
__main__.pyfile can now be executed directly by passing their name to the interpreter. The directory/zipfile is automatically inserted as the first entry in sys.path. (Suggestion and initial patch by Andy Chu; revised patch by Phillip J. Eby and Nick Coghlan; bpo-1739468.)int() 類型增加了一個(gè)
bit_length方法用來返回以二進(jìn)制代表其參數(shù)值所需的比特位數(shù)量:>>> 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 中貢獻(xiàn)。)
format() 字符串中的字段現(xiàn)在可被自動(dòng)編號:
>>> 'Sir {} of {}'.format('Gallahad', 'Camelot')'Sir Gallahad of Camelot'
之前,字符串應(yīng)當(dāng)具有所需的編號字段例如:
'Sir {0} of {1}'。(由 Eric Smith在 bpo-5237 中貢獻(xiàn))
The
string.maketrans()function is deprecated and is replaced by new static methods, bytes.maketrans() and bytearray.maketrans(). This change solves the confusion around which types were supported by the string module. Now, str, bytes, and bytearray each have their own maketrans and translate methods with intermediate translation tables of the appropriate type.(由Georg Brandl在 bpo-5675 中貢獻(xiàn))
The syntax of the with 語句的語法現(xiàn)在允許單個(gè)語句中存在多個(gè)上下文管理器:
>>> with open('mylog.txt') as infile, open('a.out', 'w') as outfile:... for line in infile:... if '' in line: ... outfile.write(line)
有了這個(gè)新語法,
contextlib.nested()函數(shù)已不再必要因而現(xiàn)在已被棄用。(由 Georg Brandl 和 Mattias Br?ndstr?m 貢獻(xiàn); appspot issue 53094。)
現(xiàn)在
round(x, n)當(dāng) x 為整數(shù)時(shí)將返回整數(shù)。 之前是返回浮點(diǎn)數(shù):>>> round(1123, -2)1100
(由 Mark Dickinson在 bpo-4707 貢獻(xiàn))
Python now uses David Gay’s algorithm for finding the shortest floating point representation that doesn’t change its value. This should help mitigate some of the confusion surrounding binary floating point numbers.
The significance is easily seen with a number like
1.1which does not have an exact equivalent in binary floating point. Since there is no exact equivalent, an expression likefloat('1.1')evaluates to the nearest representable value which is0x1.199999999999ap+0in hex or1.100000000000000088817841970012523233890533447265625in decimal. That nearest value was and still is used in subsequent floating point calculations.What is new is how the number gets displayed. Formerly, Python used a simple approach. The value of
repr(1.1)was computed asformat(1.1, '.17g')which evaluated to'1.1000000000000001'. The advantage of using 17 digits was that it relied on IEEE-754 guarantees to assure thateval(repr(1.1))would round-trip exactly to its original value. The disadvantage is that many people found the output to be confusing (mistaking intrinsic limitations of binary floating point representation as being a problem with Python itself).The new algorithm for
repr(1.1)is smarter and returns'1.1'. Effectively, it searches all equivalent string representations (ones that get stored with the same underlying float value) and returns the shortest representation.The new algorithm tends to emit cleaner representations when possible, but it does not change the underlying values. So, it is still the case that
1.1 + 2.2 != 3.3even though the representations may suggest otherwise.The new algorithm depends on certain features in the underlying floating point implementation. If the required features are not found, the old algorithm will continue to be used. Also, the text pickle protocols assure cross-platform portability by using the old algorithm.
(由 Eric Smith 和 Mark Dickinson 在 bpo-1580 貢獻(xiàn))
新增,改進(jìn)和棄用的模塊
增加了一個(gè) collections.Counter 類以支持方便地統(tǒng)計(jì)一個(gè)序列或可迭代對象中的唯一條目數(shù)量。:
>>> Counter(['red', 'blue', 'red', 'green', 'blue', 'blue'])Counter({'blue': 3, 'red': 2, 'green': 1})
(由 Raymond Hettinger 在 bpo-1696199 中貢獻(xiàn)。)
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 code implementing a widget’s behavior from the code implementing its appearance.
(由 Guilherme Polo 在 bpo-2983 中貢獻(xiàn)。)
gzip.GzipFile 和 bz2.BZ2File 類現(xiàn)在已支持上下文管理協(xié)議:
>>> # Automatically close file after writing>>> with gzip.GzipFile(filename, "wb") as f:... f.write(b"xxx")
(由 Antoine Pitrou 貢獻(xiàn)。)
decimal 模塊現(xiàn)在支持基于一個(gè)二進(jìn)制 float 來創(chuàng)建 decimal 對象。 轉(zhuǎn)換是準(zhǔn)確的但有時(shí)也會(huì)令人吃驚:
>>> Decimal.from_float(1.1)Decimal('1.100000000000000088817841970012523233890533447265625')
這個(gè)長長的 decimal 結(jié)果值顯示了 1.1 所保存的實(shí)際二進(jìn)制分?jǐn)?shù)。 這個(gè)分?jǐn)?shù)有許多位因?yàn)?1.1 無法用二進(jìn)制來精確表示。
(由Raymond Hettinger 和 Mark Dickinson貢獻(xiàn)。)
The itertools module grew two new functions. The itertools.combinations_with_replacement() function is one of four for generating combinatorics including permutations and Cartesian products. The itertools.compress() function mimics its namesake from APL. Also, the existing itertools.count() function now has an optional step argument and can accept any type of counting sequence including fractions.Fraction and decimal.Decimal:
>>> [p+q for p,q in combinations_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 貢獻(xiàn)。)
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:
>>> query = input()SELECT region, dept, count(*) FROM main GROUPBY region, dept>>> cursor.execute(query)>>> query_fields = [desc[0] for desc in cursor.description]>>> UserQuery = namedtuple('UserQuery', query_fields, rename=True)>>> pprint.pprint([UserQuery(*row) for row in cursor])[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 中貢獻(xiàn)。)
re.sub(), re.subn() 和 re.split() 函數(shù)現(xiàn)在可接受一個(gè) flags 形參。
(由 Gregory Smith 貢獻(xiàn))
The logging module now implements a simple logging.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”:
>>> h = logging.NullHandler()>>> logging.getLogger("foo").addHandler(h)
(由 Vinay Sajip 在 bpo-4384 中貢獻(xiàn)。)
支持
-m命令行開關(guān)的 runpy 模塊現(xiàn)在也支持當(dāng)提供包名稱時(shí)通過查找并執(zhí)行__main__子模塊來執(zhí)行包。(由 Andi Vajda 在 bpo-4195 中貢獻(xiàn)。)
pdb 模塊現(xiàn)在可以訪問并顯示通過 zipimport (或其他符合規(guī)范的 PEP 302 加載器) 加載的源代碼。
(由 Alexander Belopolsky 在 bpo-4201 中貢獻(xiàn)。)
functools.partial 對象現(xiàn)在可以被封存。
(由 Antoine Pitrou 和 Jesse Noller 提議,由 Jack Diederich 實(shí)現(xiàn); bpo-5228。)
為符號增加 pydoc 幫助主題以使得在交互環(huán)境下
help('@')能符合預(yù)期的效果。(由 David Laban 在 bpo-4739 中貢獻(xiàn)。)
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 a TestResult:
class TestGizmo(unittest.TestCase):@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")def test_gizmo_on_windows(self):...@unittest.expectedFailuredef test_gimzo_without_required_library(self):...
Also, tests for exceptions have been builtout to work with context managers using the with statement:
def test_division_by_zero(self):with self.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 貢獻(xiàn)。)
io 模塊新增了三個(gè)常量來代表
seek()方法SEEK_SET,SEEK_CUR和SEEK_END。sys.version_info 元組現(xiàn)在是一個(gè)具名元組:
>>> sys.version_infosys.version_info(major=3, minor=1, micro=0, releaselevel='alpha', serial=2)
(由 Ross Light 在 bpo-4285 中貢獻(xiàn)。)
The nntplib and imaplib modules now support IPv6.
(由 Derek Morr 在 bpo-1655 和 bpo-1664 中貢獻(xiàn)。)
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__.setin Python 2 is calledbuiltins.setin 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 the fix_imports option:>>> 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 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.
(由 Alexandre Vassalotti 和 Antoine Pitrou 在 bpo-6137 中貢獻(xiàn)。)
A new module, importlib was added. It provides a complete, portable, pure Python reference implementation of the import statement and its counterpart, the __import__() function. It represents a substantial step forward in documenting and defining the actions that take place during imports.
(由 Brett Cannon 貢獻(xiàn)。)
性能優(yōu)化
Major performance enhancements have been added:
The new I/O library (as defined in PEP 3116) was mostly written in Python 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 is 2 to 20 times faster depending on the task at hand. The pure Python version is still available for experimentation purposes through the
_pyiomodule.(由 Amaury Forgeot d’Arc 和 Antoine Pitrou 貢獻(xiàn)。)
Added a heuristic so that tuples and dicts containing only untrackable objects are not tracked by the garbage collector. This can reduce the size of collections and therefore the garbage collection overhead on long-running programs, depending on their particular use of datatypes.
(由 Antoine Pitrou 在 bpo-4688 中貢獻(xiàn)。)
Enabling a configure option named
--with-computed-gotoson compilers that support it (notably: gcc, SunPro, icc), the bytecode evaluation loop is compiled with a new dispatch mechanism which gives speedups of up to 20%, depending on the system, the compiler, and the benchmark.(由 Antoine Pitrou 以及其他一些參與者在 bpo-4753 中貢獻(xiàn)。)
The decoding of UTF-8, UTF-16 and LATIN-1 is now two to four times faster.
(由 Antoine Pitrou 和 Amaury Forgeot d’Arc 在 bpo-4868 中貢獻(xiàn)。)
The json module now has a C extension to substantially improve its performance. In addition, the API was modified so that json works only with str, not with bytes. That change makes the module closely match the JSON specification which is defined in terms of Unicode.
(由 Bob Ippolito 在 bpo-4136 中貢獻(xiàn)。并由 Antoine Pitrou 和 Benjamin Peterson 轉(zhuǎn)換為Py3.1)
Unpickling now interns the attribute names of pickled objects. This saves memory and allows pickles to be smaller.
(由 Jake McGuire 和 Antoine Pitrou 在 bpo-5084 中貢獻(xiàn)。)
IDLE
IDLE’s format menu now provides an option to strip trailing whitespace from a source file.
(由 Roger D. Serwy 在 bpo-5150 中貢獻(xiàn)。)
構(gòu)建和 C API 的改變
針對 Python 構(gòu)建過程和 C API 的改變包括:
Integers are now stored internally either in base
2**15or in base2**30, the base being determined at build time. Previously, they were always stored in base2**15. Using base2**30gives significant performance improvements on 64-bit machines, but benchmark results on 32-bit machines have been mixed. Therefore, the default is to use base2**30on 64-bit machines and base2**15on 32-bit machines; on Unix, there’s a new configure option--enable-big-digitsthat can be used to override this default.Apart from the performance improvements this change should be invisible to end users, with one exception: for testing and debugging purposes there’s a new sys.int_info that provides information about the internal format, giving the number of bits per digit and the size in bytes of the C type used to store each digit:
>>> import sys>>> sys.int_infosys.int_info(bits_per_digit=30, sizeof_digit=4)
(由 Mark Dickinson在 bpo-4258 貢獻(xiàn))
The PyLong_AsUnsignedLongLong() function now handles a negative pylong by raising OverflowError instead of TypeError.
(由 Mark Dickinson 和 Lisandro Dalcrin 在 bpo-5175 中貢獻(xiàn)。)
Deprecated
PyNumber_Int(). Use PyNumber_Long() instead.(由 Mark Dickinson在 bpo-4910 貢獻(xiàn))
Added a new PyOS_string_to_double() function to replace the deprecated functions
PyOS_ascii_strtod()andPyOS_ascii_atof().(由 Mark Dickinson 在 bpo-5914 貢獻(xiàn))
Added PyCapsule as a replacement for the
PyCObjectAPI. The principal difference is that the new type has a well defined interface for passing typing safety information and a less complicated signature for calling a destructor. The old type had a problematic API and is now deprecated.(由 Larry Hastings 在 bpo-5630 中貢獻(xiàn)。)
移植到 Python 3.1
This section lists previously described changes and other bugfixes that may require changes to your code:
The new floating point string representations can break existing doctests. For example:
def e():'''Compute the base of natural logarithms.>>> e()2.7182818284590451'''return sum(1/math.factorial(x) for x in reversed(range(30)))doctest.testmod()**********************************************************************Failed example:e()Expected:2.7182818284590451Got:2.718281828459045**********************************************************************
The automatic name remapping in the pickle module for protocol 2 or lower can make Python 3.1 pickles unreadable in Python 3.0. One solution is to use protocol 3. Another solution is to set the fix_imports option to
False. See the discussion above for more details.
當(dāng)前題目:創(chuàng)新互聯(lián)Python教程:Python 3.1 有什么新變化
文章出自:http://fisionsoft.com.cn/article/djhdppo.html


咨詢
建站咨詢
