python3.7和3.5_糟了!Python3.7.0 来了

导言

美国时间6月27日晚8点,Python 3.7.0 经过多轮测试,终于发布了正式版,增强了多处特性功能,同时 3.6 也更新到 3.6.6 稳定版本。

CVer

编辑: Amusi

校稿: Amusi

时间: 2018-07-08

前戏

打开Python官网,翻到 Lastet News,引入眼帘的就是 Python 3.7.0 is now available (and so is 3.6.6)! On behalf of ...

注:文末有福利

c10de6728d467b2fc0ba268cd6bd3abc.png

让我们看一下官方是怎么介绍这一爆炸性事件的...

Wednesday, June 27, 2018

Python 3.7.0 is now available (and so is 3.6.6)!

On behalf of the Python development community and the Python 3.7 release team, we are pleased to announce the availability of Python 3.7.0. Python 3.7.0 is the newest feature release of the Python language, and it contains many new features and optimizations. You can find Python 3.7.0 here:

https://www.python.org/downloads/release/python-370/

Most third-party distributors of Python should be making 3.7.0 packages available soon.

See the What’s New In Python 3.7 document for more information about features included in the 3.7 series. Detailed information about the changes made in 3.7.0 can be found in its change log. Maintenance releases for the 3.7 series will follow at regular intervals starting in July of 2018.

We hope you enjoy Python 3.7!

P.S. We are also happy to announce the availability of Python 3.6.6, the next maintenance release of Python 3.6:

https://www.python.org/downloads/release/python-366/

Thanks to all of the many volunteers who help make Python Development and these releases possible! Please consider supporting our efforts by volunteering yourself or through organization contributions to the Python Software Foundation.

简单来说就是,Python3.7.0正式发布,大家可以下载使用,其官方文档也已经发布,其中Python3.7.0版本加入了很多新功能和优化。与此同时,官方也发布了Python3.6.6。

Python3.7.0

那么Python3.7.0到底加入了哪些特性,让它如此与众不同呢?来继续往下看

主要特性

PEP 539,新增 CPython 中用于线程本地存储的 C-API

PEP 545,Python 官方文档翻译版本,新增日文、法文、韩文

PEP 552,优化 pyc 文件

PEP 553,新增内置函数 breakpoint() ,该函数在调用时自动进入调试器

PEP 557,新增内置模块dataclasses,可大幅简化类实例属性的初始化定义

PEP 560,新增支持类型模块和泛型

PEP 562,支持在模块定义 getattr 和dir

PEP 563,推迟对注释语句的分析从而优化 Python 的类型提示

PEP 564,time 内置函数支持纳秒

PEP 565,重新在 main 中默认显示 DeprecationWarning

PEP 567,新增 contextvars模块,可实现上下文变量解决变量线程安全

避免使用 ASCII 作为默认文本编码,强制 UTF-8 编码运行

字典对象的 keys 按插入顺序排列,现在是官方语言规范

多方面的显著性能优化

说这么多特性,不如举个例子吧!

这里搬运知乎大佬“口可口可”的介绍(如有侵权,立即删除)

dataclasses模块 示例

这个特性可能是 3.7.0 以后比较常用的了,是从其他语言借鉴过来的,这里简单演示下用法。

假如我们要封装一个类对象,在之前我们的代码可能要这么写:

1class Article(object):

2 def __init__(self, _id, author_id, title, text, tags=None,

3 created=datetime.now(), edited=datetime.now()):

4 self._id = _id

5 self.author_id = author_id

6 self.title = title

7 self.text = text

8 self.tags = list() if tags is None else tags

9 self.created = created

10 self.edited = edited

11

12 if type(self.created) is str:

13 self.created = dateutil.parser.parse(self.created)

14

15 if type(self.edited) is str:

16 self.edited = dateutil.parser.parse(self.edited)

17

18 def __eq__(self, other):

19 if not isinstance(other, self.__class__):

20 return NotImplemented

21 return (self._id, self.author_id) == (other._id, other.author_id)

22

23 def __lt__(self, other):

24 if not isinstance(other, self.__class__):

25 return NotImplemented

26 return (self._id, self.author_id) < (other._id, other.author_id)

27

28 def __repr__(self):

29 return '{}(id={}, author_id={}, title={})'.format(

30 self.__class__.__name__, self._id, self.author_id, self.title)

大量的初始化属性要定义默认值,可能还需要重写一堆魔法方法,来实现类实例之间的排序 去重 等功能。

如果使用dataclass进行改造,可以写成这个样子:

1@dataclass(order=True)

2class Article(object):

3 _id: int

4 author_id: int

5 title: str = field(compare=False)

6 text: str = field(repr=False, compare=False)

7 tags: List[str] = field(default=list(), repr=False, compare=False)

8 created: datetime = field(default=datetime.now(), repr=False, compare=False)

9 edited: datetime = field(default=datetime.now(), repr=False, compare=False)

10

11 def __post_init__(self):

12 if type(self.created) is str:

13 self.created = dateutil.parser.parse(self.created)

14

15 if type(self.edited) is str:

16 self.edited = dateutil.parser.parse(self.edited)

可见这种语法使代码更加简练清晰,也更符合面向对象思想的语法方式,用过 SQLAlchemy 的同学肯定觉得很像 ORM 写法。

上述示例只是最基础的展示,更丰富的用法可以查看PEP 557文档。

Python3.7.0 下载和在线文档

Python3.7.0下载

打开下载地址,哇!已经支持MacOS和Windows,估计Linux也快了!

不过Amusi更好奇Anaconda有没有推出集成Python3.7.0的软件包

a55b345050efc63ac525ccf19838e10b.png

Python3.7.0 在线文档

打开Python3.7.0 在线文档,新特性介绍、教程和安装模块等一网打尽,Python的社区维护真的相当好

414eb5cbecd9b7f9b00cddde2854cc39.png

Amusi 作为还在使用Python2.7和Python3.5的程序猿,其实感到很开森,新版本无疑更强大更优化,大家也快更新代码~适应新环境

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值