Python 3.6部分新特性
2016-12-26 20:00
363 查看
Python 3.6.0于2016年12月23日正式发布,除了本文提到的这一部分比较重要的新特性之外,还有很多标准库也新增了部分功能,等有时间再慢慢整理。
1、内置字典对象dict进行了重新设计,可以节约20%到25%的内存
2、格式化的字符串常量,在字符串前面加字母f,含义与format()方法类似,但是更简洁
>>> name = 'Dong'
>>> age = 39
>>> f'My name is {name}, and I am {age} years old.'
'My name is Dong, and I am 39 years old.'
>>> width = 10
>>> precision = 4
>>> value = 11/3
>>> f'result:{value:{width}.{precision}}'
'result: 3.667'
3、数字常量中间位置支持使用下划线来提高可读性,类似于数学上的逗号作为千位分隔符
>>> 1_000_000
1000000
>>> 1_2_3_4
1234
>>> 1_2 + 3_4j
(12+34j)
同时,字符串格式化方法format()也提供了对下划线的支持
>>> '{0:_},{0:_x}'.format(1000000)
'1_000_000,f_4240'
>>> '{0:_},{0:_x}'.format(10000000)
'10_000_000,98_9680'
4、支持异步生成器函数,可以在同一个协程函数中同时使用await和yield
import asyncio
async def ticker(delay, to):
for i
in range(to):
yield i
await asyncio.sleep(delay)
async def run():
async for i
in ticker(1, 10):
print(i)
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(run())
finally:
loop.close()
5、允许在协程函数中异步推导式
async def ticker(delay, to):
for i
in range(to):
yield i
await asyncio.sleep(delay)
async def run():
result = [i async
for i in ticker(1, 10)
if i%2]
print(result)
import asyncio
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(run())
finally:
loop.close()
1、内置字典对象dict进行了重新设计,可以节约20%到25%的内存
2、格式化的字符串常量,在字符串前面加字母f,含义与format()方法类似,但是更简洁
>>> name = 'Dong'
>>> age = 39
>>> f'My name is {name}, and I am {age} years old.'
'My name is Dong, and I am 39 years old.'
>>> width = 10
>>> precision = 4
>>> value = 11/3
>>> f'result:{value:{width}.{precision}}'
'result: 3.667'
3、数字常量中间位置支持使用下划线来提高可读性,类似于数学上的逗号作为千位分隔符
>>> 1_000_000
1000000
>>> 1_2_3_4
1234
>>> 1_2 + 3_4j
(12+34j)
同时,字符串格式化方法format()也提供了对下划线的支持
>>> '{0:_},{0:_x}'.format(1000000)
'1_000_000,f_4240'
>>> '{0:_},{0:_x}'.format(10000000)
'10_000_000,98_9680'
4、支持异步生成器函数,可以在同一个协程函数中同时使用await和yield
import asyncio
async def ticker(delay, to):
for i
in range(to):
yield i
await asyncio.sleep(delay)
async def run():
async for i
in ticker(1, 10):
print(i)
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(run())
finally:
loop.close()
5、允许在协程函数中异步推导式
async def ticker(delay, to):
for i
in range(to):
yield i
await asyncio.sleep(delay)
async def run():
result = [i async
for i in ticker(1, 10)
if i%2]
print(result)
import asyncio
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(run())
finally:
loop.close()
相关文章推荐
- Python 3 初探,第 1 部分: Python 3 的新特性
- Atitit python3.0 3.3 3.5 3.6 新特性 Python2.7新特性1Python 3_x 新特性1python3.4新特性1python3.5新特性1值得关注的新特性1Pyth
- Python 3 初探,第 1 部分: Python 3 的新特性
- Atitit python3.0 3.3 3.5 3.6 新特性 Python2.7新特性1Python 3_x 新特性1python3.4新特性1python3.5新特性1值得关注的新特性1Python3.6新特性2 Python2.7新特性Python 2.7的新特性 - 牛皮糖NewPtone - 博客园.html Python 3_x 新特性及10大变化_python_脚本之家.htm
- Python3.6新特性
- 十五分钟让你了解Python套路 ---- Python的基本语法和部分特性
- Python2.4新特性(部分)
- Python3.6新特性官方文档中文版
- python3.6 使用pycha进行图表绘制(由于pycha长期未更新无法直接兼容python3.6,需做部分修改)
- 【python】| python3.6新特性整理
- Python 3 初探,第 1 部分: Python 3 的新特性
- Atitit python3.0 3.3 3.5 3.6 新特性 Python2.7新特性1Python 3_x 新特性1python3.4新特性1python3.5新特性1值得关注的新特性1Pyth
- Python3.6正式版新特性预览
- Python3.6新特性
- IBM Informix Dynamic Server 11 系统管理(考试 918)认证指南,第 8 部分: 面向管理员的 SQL 特性
- Lucene的特性分析 -- lucene的核心部分
- Windows Vista的部分安全特性与金山毒霸Vista版
- Python公布版本升级日程表,诸多特性改变
- struts1.2.7 的部分新加特性
- 探索 Python,第 7 部分: 探索 Python 类型层次结构