Python 3.6 有什么新特性

作者&投稿:弋胁 (若有异议请与网页底部的电邮联系)
Python 3.6 有什么新特性~

首先3.6实现了16个新PEP标准,其中几个值得特别说一下:
PEP-0498 Formatted string literals,这个已经有人提过了。
PEP-0530和PEP-0525为async/await扩展了新的应用场景,不过我觉得前面应该比后面更常用一点。
PEP-0526 Syntax for variable annotations,是专门为Type Hint添加的,这会为Python的大型工程化表现有所帮助,建议大家一起来把类型声明一下吧。我们已经在内部项目中使用了TypeHint,还是比较有用的。
PEP-0529和PEP-0528都是为Windows用户准备的,全部切换成了UTF8,不过我不是Windows用户...
一个非PEP标准:
DTrace and SystemTap probing support,这是为Python添加了本地调试器支持,这个会对Python的开发尤其是服务器之类的场景非常有帮助。今年pycon china shanghai来自饿了么的工程师也分享了相关的黑魔法(clouddn.com 的页面),之前是需要特定版本Python支持的功能现在在Linux和macOS上都可以支持了。

PEP 498: Formatted string literals
PEP 498 introduces a new kind of string literals: f-strings, orformatted string literals.

Formatted string literals are prefixed with 'f' and are similar tothe format strings accepted by str.format(). They contain replacementfields surrounded by curly braces. The replacement fields are expressions,which are evaluated at run time, and then formatted using theformat() protocol:
>>> 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'PEP 526: Syntax for variable annotations
PEP 484 introduced the standard for type annotations of functionparameters, a.k.a. type hints. This PEP adds syntax to Python for annotatingthe types of variables including class variables and instance variables:
primes: List[int] = []captain: str # Note: no initial value!class Starship: stats: Dict[str, int] = {}Just as for function annotations, the Python interpreter does not attach anyparticular meaning to variable annotations and only stores them in the__annotations__ attribute of a class or module.

In contrast to variable declarations in statically typed languages,the goal of annotation syntax is to provide an easy way to specify structuredtype metadata for third party tools and libraries via the abstract syntax treeand the __annotations__ attribute.
PEP 515: Underscores in Numeric Literals
PEP 515 adds the ability to use underscores in numeric literals forimproved readability. For example:
>>> 1_000_000_000_000_0001000000000000000>>> 0x_FF_FF_FF_FF4294967295Single underscores are allowed between digits and after any basespecifier. Leading, trailing, or multiple underscores in a row are notallowed.

The string formatting language also now has supportfor the '_' option to signal the use of an underscore for a thousandsseparator for floating point presentation types and for integerpresentation type 'd'. For integer presentation types 'b','o', 'x', and 'X', underscores will be inserted every 4digits:
>>> '{:_}'.format(1000000)'1_000_000'>>> '{:_x}'.format(0xFFFFFFFF)'ffff_ffff'copy自官网,剩下的我不贴出来了,自己去官网看吧:https://docs.python.org/dev/whatsnew/3.6.html

有哪些重要的新特性。

1. 格式化字符串字面量

PEP 498引入了 f-string,一种新型的字符串字面量。中文翻译为“格式化字符串字面量”。

这种字符串以 f 为前缀,类似 str.format() 方法所接受的字符串。其中的可替换字段用 {} 包裹起来,在运行时进行求值。

具体代码示例:

>>> width = 10
>>> precision = 4
>>> value = decimal.Decimal("12.34567")
>>> f"result: {value:{width}.{precision}}" # nested fields
'result: 12.35'
2. 变量注释语法

此前,Python 已加入了对函数变量类型进行注释的标准,也就是 type hint。而 Python 3.6 中则根据PEP 526的提议,加入了对更多变量类型注释的功能,包括类变量和实例变量。

具体代码示例:

captain: str # 未设置初始值
class Starship:
stats: Didct[str, int] = {}
与静态语言中的变量声明不同,Python 中的变量声明是为了更加方便地位第三方工具和库提供结构化的类型元数据。会使用到新语法的工具包括:mypy,pytype,PyCharm,等等。

3. 数字字面量使用下划线

对于较大的数字来说,位数太多可能不好判断值到底有多大。现在新版本中将允许你在数字字面量中使用下划线,提高可读性。

具体代码示例:

>>> 1_000_000_000_000_000
1000000000000000
>>> 0x_FF_FF_FF_FF
4294967295
4. 异步生成器

在上一个版本中,Python 引入了对原生协程的支持,并可使用 async 或 await 语法,但是有一个限制是没办法在同一个函数体中使用 await 和 yield 。这个限制在 3.6 版中取消了,因此以后将可以定义异步生成器。

具体代码示例:

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)
使用新语法,可以让你的代码更简洁,运行速度更快。

5. 异步推导

推导(Comprehension)本身就是 Python中一个很棒的语法糖。在新版本中,它将得到一次重大升级。PEP 530提出了在列表、元组、字典推导或生成器表达式中使用 async for 语法。

这样就将原有各种推导式变成了可支持异步。

同时,推导式中还支持使用 await 表达式。

以上就是 3.6 版本中新增的 5 大特性:

格式化字符串字面量

变量注释语法

数字字面量使用下划线

异步生成器

异步推导


旬邑县13077212041: Python 3.6 有什么新特性 -
桓阳匹多: 有哪些重要的新特性.1. 格式化字符串字面量PEP 498引入了 f-string,一种新型的字符串字面量.中文翻译为“格式化字符串字面量”.这种字符串以 f 为前缀,类似 str.format() 方法所接受的字符串.其中的可替换字段用 {} 包裹起来,...

旬邑县13077212041: python高级特性知多少 -
桓阳匹多: python语言的一些高阶用法主要有以下几个特性: 1 generators生成器用法 2 collections包常见用法 3 itertools包常见用法 4 packing/unpacking封包/解包特性 5 Decorators装饰器 6 Context Managers上下文管理期 以上几个特性我会针对应用场景...

旬邑县13077212041: Python的特点有哪些特点 -
桓阳匹多: Python是一种计算机程序设计语言.是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的、大型项目的开发. Python的特点如下: 1、简单 Python是一...

旬邑县13077212041: 升级到Python 3.6后,需要注意些什么 -
桓阳匹多: 值得以后注意.具体主要包括以下几个方面.1、新关键字 Python 3.5中引入了async和await,不建议将这两个名称用作变量名、类名、函数名和模块名.它们将在Python 3.7中正式成为关键字.2、废弃的方法 mportlib.machinery....

旬邑县13077212041: 新的Python 3.4版本有哪些新的玩意 -
桓阳匹多: 现在都出python3.6了,这个问题好早了吧 要想知道哪个版本的新功能,可以去python的网站去看更新日志3.4更新 新的语法特点:没有新的语法特征是Python 3.4增加.其它新的功能:PIP应该始终可用.新创建的文件描述符是非遗传性.命令行...

旬邑县13077212041: 详解如何在 Linux 中安装最新的 Python 3.6 版本 -
桓阳匹多: 在 CentOS 7 中安装 Python 之前,请确保系统中已经有了所有必要的开发依赖:# yum -y groupinstall development# yum -y install zlib-devel 在 Debian 中,我们需要安装 gcc、make 和 zlib 压缩/解压缩库:# aptitude -y install gcc make zlib1g-dev ...

旬邑县13077212041: python3.6系列区别大吗 -
桓阳匹多: 其实没有特别大的区别,就是一些功能上的更新版本不同.

旬邑县13077212041: 如何在Windows7系统上安装最新的64位Python3.6 -
桓阳匹多: 方法/步骤第一步,下载最新版Python3.6.0,双击exe安装文件,执行安装程序; (1)现在安装 (2)自定义安装 安装对所有用户,添加Python3.6到PATH路径中,第二步,选择必须的特性,可以勾选所有的选项,然后点击“Next”进入下一步,第三步,下面勾选高级选项,并选择安装路径,也可以选择默认路径,第四步,下面进入安装过程,界面显示安装过程中,需要等待一段时间,第五步,安装完成后,提示安装成功后,关闭窗口,代表安装成功,第六步,验证最新版Python是否安装成功,打开Python命令窗口.

旬邑县13077212041: Python 3.5 有什么新特性 -
桓阳匹多: dynamic 在新版本的C#中,dynamic关键词是一个很重要的新特性,现在你可以创建动态对象并在运行时再决定它的类型 可选(或默认)参数 貌似这个特性在C#1.0就已经有很多人问过了,但直到4.0才有.现在你可以在方法定义的时候为参数指定一个默认值...

旬邑县13077212041: python3.6和python2.7的区别 -
桓阳匹多: Python 3.x引入了一些与Python 2不兼容的关键字和特性,在Python 2中,可以通过内置的__future__模块导入这些新内容.如果你希望在Python 2环境下写的代码也可以在Python 3.x中运行,那么建议使用__future__模块.例如,如果希望在...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网