Python3.0-3.6的版本变化
Table of Contents
Python3.0
简单的变化
- Print Is A Function - print现在是一个- function而不是一个关键字了, 对于这个变化我只能说 ⑥
- Views And Iterators Instead Of Lists - 某些接口的返回值由 - List替换为了- View或- Iterators:- 字典 - dict的方法- dict.keys(),- dict.items()和- dict.values()现在返回的是- View而不是- List了
- 内置函数 - map()和- filter()现在返回- Iterators而不是返回- List
- range()的行为变得和- xrange()一样, 而- xrange()惨遭移除 QAQ
- zip()现在返回的也是- Iterators了
 - 感觉这些变化适应了就挺好的, 还可以在一定程度上提升 - Python的性能, 就是苦了那些需要兼容- Python2的程序 @_@
- Ordering Comparisons - 比较操作符 - <,- <=,- >=和- >现在比较两个无法比较的对象时会引发- TypeError, 比如- 0 > None之类的操作。 不包括- !=和- ==
 这两个操作符。- Python2居然可以这样比较……
- 内置函数 - builtin.sorted()和- list.sort()不在支持- cmp参数。- 让坑过…… 
- 不在支持 - cmp()和- __cmp__(), 现在使用- __lt__()进行排序,- __eq__()和- __hash__()用于比较。- 这个没啥存在感 @_@ 
 
- Text Vs. Data Instead Of Unicode Vs. 8-bit - 老话题了, - byte变成了- unicode, 不过用起来感觉不错
语法的变化
新语法
- PEP 3107 – Function Annotations - 现在函数的参数和返回值都可以有注释了, 但是貌似没看到有人用, 有兴趣的可以了解一下: - def compile(source: "something compilable",
 filename: "where the compilable thing comes from",
 mode: "is this a single statement or a suite?"):
 ... def haul(item: Haulable, *vargs: PackAnimal) -> Distance:
 ...
 
- PEP 3102 – Keyword-Only Arguments - def compare(a, b, *ignore, key=None):
 if ignore: # If ignore is not empty
 raise TypeError
 - 类似上面代码的代码现在可以用如下方式代替: - def compare(a, b, *, key=None):
 ...
 
- PEP 3104 – Access to Names in Outer Scopes - 新的关键字 - nonlocal, 可以声明一个变量不是本地变量, 具体使用可以去看文档。
- PEP 3132 – Extended Iterable Unpacking - 扩展的可迭代包, 原来这样的语法是 - 3.0出来的:- (a, *rest, b) = range(5)
 - 其实感觉用的也不多, 但是确实是一个很不错的特性 
- 
字典推导式 的正确证明, 莫非当初只是提出了 PEP 274但一时没实现 ?反正感觉编写文档的作者挺高兴的 <_< 
- 集合字面值 - 也就是说类似下面的字面值表示一个集合: - {1, 2}
 - 同时, 集合推导式 也自然而然的出现了: - {x for x in stuff}
 
- 新的八进制字面值 - 类似 - 0o720的数字代表八进制数字, 早在- 2.6就有了
- 新的二进制字面值 - 类似 - 0b1010的数字代表二进制数字, 也是- 2.6就有了, 同时新增内置函数- bin()
- 字节字符串 - 现在使用前导字符 - b或- B来表示一个字节字符串:- b'123456'
 - 同时, 新增内置函数 - bytes()
改动的语法
- 新的 - raise语法- raise [expr [from expr]]
 - 额, 感觉没人用的样子 
- 这些东西现在是保留字: - True,- False,- None,- with,- as
- PEP 3110 – Catching Exceptions in Python 3000 - 就是这个: - try:
 try_body
 except E, N:
 except_body
 - 变成了: - try:
 try_body
 except E as N:
 except_body
 
- PEP 3115 – Metaclasses in Python 3000 - 元类的指定方式变了: - # Python2
 class C:
 __metaclass__ = M
 ... # Python3
 class C(metaclass=M):
 ...
 - 贼坑, 如果要兼容可以使用 - with_metaclass
剩下的变化
这次的整理主要是了解一些新特性和常用的变化, 对于哪些说出去基本没人知道的特性的变化还是算了吧……
因此, 剩下的变化我选择省略, 如果你有兴趣的话, 可以到 官方文档 了解一波。
另外, 这里有一个很有趣的事情:

Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> "%s" % 123456
'123456'
@_@
Python3.1
相对 3.0 来说, 3.1 的变化不是很多, 简单来看看。
- PEP 372 – Adding an ordered dictionary to collections - In [1]: from collections import OrderedDict In [2]: d = OrderedDict() In [3]: d['parrot'] = 'dead' In [4]: d['penguin'] = 'exploded' In [5]: d.items()
 Out[5]: odict_items([('parrot', 'dead'), ('penguin', 'exploded')])
 - 有序字典, 这个在一些需要保持原有顺序的地方很有用。 
- PEP 378: Format Specifier for Thousands Separator - In [1]: format(1234567, ',d')
 Out[1]: '1,234,567' In [2]: format(1234567.89, ',.2f')
 Out[2]: '1,234,567.89' In [3]: format(12345.6 + 8901234.12j, ',f')
 Out[3]: '12,345.600000+8,901,234.120000j'
 - 都快忘记有 - format这个内置函数了 QAQ- 感觉这个特性还是挺好的, 用到的时候可以省不少劲。 
- new method bitlength() for int type - >>> n = 37
 >>> bin(n)
 '0b100101'
 >>> n.bit_length()
 6
 
虽然还有一些其他的变化, 但我觉得剩下的变化简单了解一下就好, 就不列出来了。
Python3.2
和 3.1 一样, 变换不是很多, 简单看一下就好。
- PEP 3147: PYC Repository Directories - 新的缓存机制, 以前的缓存方案为 - .pyc文件, 但是这个方案在安装了多个- Python的机器上
 运行的不太好。- 于是现在更换的缓存机制, 将缓存保存在了 - __pycache__目录中, 同时根据- Python版本命名缓存文件。- 目录 - __pycache__基本上一出现我就直接删除了 QAQ
- new start option -q - 使用 - Python -q启动解释器可以不显示版本信息, 感觉对我来说暂时没啥用。
- range objects now support index and count methods - >>> range(0, 100, 2).count(10)
 1
 >>> range(0, 100, 2).index(10)
 5
 >>> range(0, 100, 2)[5]
 10
 >>> range(0, 100, 2)[0:5]
 range(0, 10, 2)
 - 我觉得这是一个很棒的特性 ( ̄▽ ̄)/ 
- The callable() builtin function from Py2.x was resurrected - 什么, - callable()GG 过 !!!∑(゚Д゚ノ)ノ
剩下的变化主要是关于内置模块和低层接口的, 这些东西还是在实践中看相关的文档好了。
Python3.3
和前面两个版本一样, 核心的语法变化不是很多, 简单来看一下就好。
- PEP 397 – Python launcher for Windows - 如果你的操作系统是 - Windows, 并且安装了不止一个版本的- Python, 那么这个特性应该不陌生。- 通过如下的指令来启动指定版本的 - Python解释器:- py -2.7
 py -3.6
 
- PEP 380 – Syntax for Delegating to a Subgenerator - 我觉的这是个好东西, 通过文档中的一个例子来看: - >>> def g(x):
 ... yield from range(x, 0, -1)
 ... yield from range(x)
 ...
 >>> list(g(5))
 [5, 4, 3, 2, 1, 0, 1, 2, 3, 4]
 - 这个很爽啊, 又可以少写几行代码 @_@ 
- PEP 409 – Suppressing exception context - 这个特性可以让异常的输出更加清楚 ? - >>> class D:
 ... def __init__(self, extra):
 ... self._extra_attributes = extra
 ... def __getattr__(self, attr):
 ... try:
 ... return self._extra_attributes[attr]
 ... except KeyError:
 ... raise AttributeError(attr) from None
 ...
 >>> D({}).x
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "<stdin>", line 8, in __getattr__
 AttributeError: x
 - 关键点在 - from None上, 没有这个- from None的话, 抛出的异常会是这样的:- Traceback (most recent call last):
 File "<stdin>", line 6, in __getattr__
 KeyError: 'x' During handling of the above exception, another exception occurred: Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "<stdin>", line 8, in __getattr__
 AttributeError: x
 
- PEP 414 – Explicit Unicode Literal for Python 3.3 - 这个特性运行在 - Python3的版本环境中使用- u前缀显示的声明- unicode字符串:- u'text'
 U'text'
 - 虽然已经习惯了, 但还是想吐槽 - 2 -> 3的版本变化是真的阔怕……
- PEP 3155 – Qualified name for classes and functions - 函数和类对象的新属性: - __qualname__, 效果看代码:- >>> class C:
 ... class D:
 ... def meth(self):
 ... pass
 ...
 >>> C.D.__name__
 'D'
 >>> C.D.__qualname__
 'C.D'
 >>> C.D.meth.__name__
 'meth'
 >>> C.D.meth.__qualname__
 'C.D.meth'
 
Python3.4
这个版本的文档结构比前面几个版本清楚多了, 简单看一下新特性。
- PEP 453 – Explicit bootstrapping of pip in Python installations - 这个特性很好, 很 - nice, 现在你可以通过这样的命令来安装- pip了:- python -m ensurepip
 - 之前我的 - pip因为一天命令 GG, 也因为一条指令还原 QAQ
emmmm, 剩下的变化对我们写代码的影响不大, 省略。
Python3.5
- PEP 492 – Coroutines with async and await syntax - 针对 协程 的新关键字 - async和- await, 官网的例子:- import asyncio async def coro(name, lock):
 print('coro {}: waiting for lock'.format(name))
 async with lock:
 print('coro {}: holding the lock'.format(name))
 await asyncio.sleep(1)
 print('coro {}: releasing the lock'.format(name)) loop = asyncio.get_event_loop()
 lock = asyncio.Lock()
 coros = asyncio.gather(coro(1, lock), coro(2, lock))
 try:
 loop.run_until_complete(coros)
 finally:
 loop.close() # results
 coro 2: waiting for lock
 coro 2: holding the lock
 coro 1: waiting for lock
 coro 2: releasing the lock
 coro 1: holding the lock
 coro 1: releasing the lock
 - 我觉得这是一个很重要的新特性, 目前在 - Github上已经可以看到一些只针对高版本- Python开发的库,
 其中就用到了这一特性。- 当然了, 目前我对协程的了解并不多, 因此也更应该去了解。 嗯, 现在知道这么一个特性了。 
- PEP 465 – A dedicated infix operator for matrix multiplication - 这应该算是语言的应用场景反过来影响语法开发的一个实例吧, 针对 矩阵运算 的操作符 - @.- >>> import numpy >>> x = numpy.ones(3)
 >>> x
 array([ 1., 1., 1.]) >>> m = numpy.eye(3)
 >>> m
 array([[ 1., 0., 0.],
 [ 0., 1., 0.],
 [ 0., 0., 1.]]) >>> x @ m
 array([ 1., 1., 1.])
 - 这些东西都没用过, 不做评价。 有兴趣的可以了解一下。 
- PEP 448 – Additional Unpacking Generalizations - 扩展了 - *iterable和- **dictionary拆包操作符的允许用法, 现在可以在函数中调用任意数量的拆包:- >>> print(*[1], *[2], 3, *[4, 5])
 1 2 3 4 5 >>> def fn(a, b, c, d):
 ... print(a, b, c, d)
 ... >>> fn(**{'a': 1, 'c': 3}, **{'b': 2, 'd': 4})
 1 2 3 4
 - 同时, 元组, 列表, 集合, 字典等允许同时多个解包: - >>> *range(4), 4
 (0, 1, 2, 3, 4) >>> [*range(4), 4]
 [0, 1, 2, 3, 4] >>> {*range(4), 4, *(5, 6, 7)}
 {0, 1, 2, 3, 4, 5, 6, 7} >>> {'x': 1, **{'y': 2}}
 {'x': 1, 'y': 2}
 - 恐怖如斯 ! 
- 
和这个特性有关的一个特性: PEP 3107. 我觉得, 只要看了它的表现形式你也应该明白是和什么有关了: def greeting(name: str) -> str:
 return 'Hello ' + name
 通过注释来表示参数和返回值的类型。 效果: >>> def greeting(name: str) -> str:
 ... return 'Hello ' + name
 ...
 >>> greeting(10)
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "<stdin>", line 2, in greeting
 TypeError: must be str, not int
 感觉 Python代码长得越来越奇怪了 QAQ
Python3.6
- PEP 498 – Literal String Interpolation - 现在我学过的脚本语言都会这一套了: - >>> name = "Fred"
 >>> f"He said his name is {name}."
 'He said his name is Fred.'
 >>> width = 10
 >>> precision = 4
 >>> value = decimal.Decimal("12.34567")
 >>> f"result: {value:{width}.{precision}}" # nested fields
 'result: 12.35'
 - 感觉通过 - Python的 名称空间 来实现这个特性还是很方便的, 不知道是不是这样来实现的。
- PEP 526 – Syntax for Variable Annotations - 通过特殊的语法来对变量的类型进行注释: - primes: List[int] = [] captain: str # Note: no initial value! class Starship:
 stats: Dict[str, int] = {}
 - 其他的注释也可以: - name: 'your name'
 - 但似乎就只有注释的作用 ? - >>> name: str = 10
 
- PEP 515 – Underscores in Numeric Literals - 通过在数字中添加下划线来提高可读性: - >>> 1_000_000_000_000_000
 1000000000000000
 >>> 0x_FF_FF_FF_FF
 4294967295
 
- PEP 525 – Asynchronous Generators - 3.5引入了关键字- async和- await, 但是, 通过- async修饰的函数内部不能在使用- yield, 现在, 这个限制
 解除了:- async def ticker(delay, to):
 """Yield numbers from 0 to *to* every *delay* seconds."""
 for i in range(to):
 yield i
 await asyncio.sleep(delay)
 - 代码更难懂了…… 
- PEP 530 – Asynchronous Comprehensions - 现在在各种推导式, 生成式中都可以用 - async了:- result = [i async for i in aiter() if i % 2]
 result = [await fun() for fun in funcs if await condition()]
 
- PEP 487 – Simpler customisation of class creation - 现在可以在不使用元类的情况下自定义子类创建, 通过方法 - __init_subclass__:- >>> class QuestBase:
 ... # this is implicitly a @classmethod (see below for motivation)
 ... def __init_subclass__(cls, swallow, **kwargs):
 ... cls.swallow = swallow
 ... super().__init_subclass__(**kwargs) >>> class Quest(QuestBase, swallow="african"):
 ... pass >>> Quest.swallow
 'african'
 
- PEP 487 – Simpler customisation of class creation - PEP 487除了前面那个特性以外, 还增强了 描述器 协议 (ノ`Д)ノ- class IntField:
 def __get__(self, instance, owner):
 return instance.__dict__[self.name] def __set__(self, instance, value):
 if not isinstance(value, int):
 raise ValueError(f'expecting integer in {self.name}')
 instance.__dict__[self.name] = value # this is the new initializer:
 def __set_name__(self, owner, name):
 self.name = name class Model:
 int_field = IntField()
 - __set_name__这个方法会在描述器定义时自动定义,- owner是这个描述器的拥有者,- name则是
 描述器在- owner中的名称。- 在上述例子中, - name就是- int_field.
- PEP 529: Change Windows filesystem encoding to UTF-8 
- PEP 528: Change Windows console encoding to UTF-8 
- PEP 520 – Preserving Class Attribute Definition Order - 现在类的 - __dict__中的属性顺序和定义顺序保持一致了。
- PEP 468 – Preserving the order of **kwargs in a function - **kwargs形式的参数现在会保持传入的顺序:- >>> def func(**kwargs):
 ... print(kwargs)
 ...
 >>> func(a=1, b=2, c=3, ab=4)
 {'a': 1, 'b': 2, 'c': 3, 'ab': 4}
 
相关链接
- What’s New In Python 3.0
- What’s New In Python 3.1
- What’s New In Python 3.2
- What’s New In Python 3.3
- What’s New In Python 3.4
- What’s New In Python 3.5
- What’s New In Python 3.6
Python3.0-3.6的版本变化的更多相关文章
- 【和我一起学Python吧】Python3.0与2.X版本的区别
		做为一个前端开发的码农,却正在阅读最新版的<A byte of Python>.发现Python3.0在某些地方还是有些改变的.准备慢慢的体会,与老版本的<A byte of Pyt ... 
- 谨慎安装Python3.7.0,SSL低版本导致Pip无法使用
		最新新配置了一台服务器.安装 的时候直接使用了最新的Python 3.7最新版本. 安装成功,编译成功.但是用pip 安装包的时候提示:pip is configured with locations ... 
- Ubuntu18.04LTS python3.6 cuda10.0 下安装低版本的pytorch
		Ubuntu18.04LTS python3.6 cuda10.0 下安装低版本的pytorch,运行Hypergraph Neural Networks(HGNN) https://github.c ... 
- Atitit python3.0 3.3 3.5 3.6 新特性 Python2.7新特性1Python 3_x 新特性1python3.4新特性1python3.5新特性1值得关注的新特性1Pyth
		Atitit python3.0 3.3 3.5 3.6 新特性 Python2.7新特性1 Python 3_x 新特性1 python3.4新特性1 python3.5新特性1 值得关注的新特性1 ... 
- 【转载】python3.0与2.x之间的区别
		python3.0与2.x之间的区别: 1.性能 Py3.0运行pystone benchmark的速度比Py2.5慢30%.Guido认为Py3.0有极大的优化空间,在字符串和整形操作上可以取得很好 ... 
- python3.0与2.x之间的区别
		python3.0与2.x之间的区别: 1.性能 Py3.0运行pystone benchmark的速度比Py2.5慢30%.Guido认为Py3.0有极大的优化空间,在字符串和整形操作上可以取得很好 ... 
- 相比于python2.6,python3.0的新特性。
		这篇文章主要介绍了相比于python2.6,python3.0的新特性.更详细的介绍请参见python3.0的文档. Common Stumbling Blocks 本段简单的列出容易使人出错的变动. ... 
- Python3.0 调用HTMLTestRunner生成的报告中不能显示用例中print函数的输出
		官方原生的HTMLTestRunner.py支持python2.0版本,python3.0版本的使用需要做一些修改: Python3调用HTMLTestRunner执行用例生成测试报告中,不能正常显示 ... 
- 【RTOS】基于V7开发板的最新版uCOS-III V3.07.03程序模板,含MDK和IAR,支持uC/Probe,与之前版本变化较大
		模板下载: 链接:https://pan.baidu.com/s/1_4z_Lg51jMT87RrRM6Qs3g 提取码:2gns 对MDK的AC6也做了支持:https://www.cnblog ... 
随机推荐
- 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:0.概述
			欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 序言 帖主和队友仿制了一个简单版的微信,其中,队友是用Unity3D做前段,帖主用Java的Mina.Hiberna ... 
- 触发OOM杀掉了mysql
			中午收到反馈平台所有账号全部无法登录,运维就是苦逼,饭都没吃就跑来处理紧急故障,先自己测试了下确实无法登录进系统,登录服务器检查,发现mysql数据库挂掉了,定位到了原因就赶紧重启mysql吧,结果启 ... 
- mif文件生成方法
			mif文件就是存储器初始化文件,即memory initialization file,用来配置RAM或ROM中的数据.常见生成方法: Quartus自带的mif编辑器生成 mif软件生成 高级编程语 ... 
- php生成纯数字、字母数字、图片、纯汉字的随机数验证码
			现在讲开始通过PHP生成各种验证码旅途,新手要开车了,请刷卡! 首先,我们开始先生成一个放验证码的背景图片 注:没有Imagejpg()这个函数,只有imagepng()函数 imagecreatet ... 
- Redis单机数据库
			单机数据库 ·Redis服务器的所有数据库都保存在redisServer.db数组中,而数据库的数量则由redisServer.dbnum属性保存. ·客户端通过修改目标数据库指针,让它指向redis ... 
- JavaScript: apply , call 方法
			我在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这里我做如下笔记,希望和大家 ... 
- IOS tableView 去除分割线 和 不允许选中表格cell
			//去除分割线 self.tableView.backgroundColor=[UIColor colorWithRed:///255.0 alpha:1.0]; self.tableView.sep ... 
- go语言,安装包fetch error 问题解决方案
			最近需要安装grequests,出现了下面的error [fdf@zxmrlc ~]$ go get github.com/levigross/grequests package golang.org ... 
- 解决session过期跳转到登录页并跳出iframe框架(或者layui弹出层)
			当用户长时间停留在管理界面没有操作,等到session过期后,进行了操作,那么只是iframe跳转到login页面,这不是我们想要的结果.解决方法:在login页面加一个逻辑判断: <scrip ... 
- Websocket教程SpringBoot+Maven整合(详情)
			1.大话websocket及课程介绍 简介: websocket介绍.使用场景分享.学习课程需要什么基础 笔记: websocket介绍: WebSocket协议是基于TCP的一种新的网络协议.它实现 ... 
