diff --git a/README.md b/README.md index b8b45a2..86886e2 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,8 @@ ### python_aiohttp.py: Python中最好用的异步爬虫库Aiohttp代码实例 ### python_thread_multiprocess.py: Python进阶: 聊聊IO密集型任务、计算密集型任务,以及多线程、多进程 + +### python_version36.py: Python3.6正式版要来了, 你期待哪些新特性? =================================================================================================== ### 您可以fork该项目,并在修改后提交Pull request diff --git a/python_version36.py b/python_version36.py new file mode 100644 index 0000000..85b3f3f --- /dev/null +++ b/python_version36.py @@ -0,0 +1,52 @@ +# _*_ coding: utf-8 _*_ + +""" +python_version36.py by xianhu +""" + +import asyncio +import decimal +from typing import List, Dict + +# Formatted string literals +name = "Fred" +print(f"He said his name is {name}.") # 'He said his name is Fred.' +print("He said his name is {name}.".format(**locals())) + +width = 10 +precision = 4 +value = decimal.Decimal("12.34567") +print(f"result: {value:{width}.{precision}}") #'result: 12.35' + + +# variable annotations +def test(a: List[int], b: int) -> int: + return a[0] + b +print(test([3, 1], 2)) + +primes: List[int] = [] +captain: str + +class Starship: + stats: Dict[str, int] = {} + + +# Underscores in Numeric Literals +a = 1_000_000_000_000_000 # 1000000000000000 +b = 0x_FF_FF_FF_FF # 4294967295 + +'{:_}'.format(1000000) # '1_000_000' +'{:_x}'.format(0xFFFFFFFF) # 'ffff_ffff' + + +# Asynchronous Generators +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) + + +# Asynchronous Comprehensions +result = [i async for i in aiter() if i % 2] +result = [await fun() for fun in funcs if await condition()]