Python 3
What's new in Python 3.x¶
nonlocal / global¶
x = 0
def outer():
x = 1
def inner():
nonlocal x
x = 2
print("inner:", x)
inner()
print("outer:", x)
outer()
print("global:", x)
# inner: 2
# outer: 2
# global: 0
## with global
x = 0
def outer():
x = 1
def inner():
global x
x = 2
print("inner:", x)
inner()
print("outer:", x)
outer()
print("global:", x)
# inner: 2
# outer: 1
# global: 2
String interpolation - new in 3.6¶
name="David"
f"My name is {name}"
value = decimal.Decimal("10.4507")
print(f"result: {value:10.5}" ) # width precision
PEP 492 - Coroutines with async and await syntax¶
yield from iterator
is equivalent
Example:
def lazy_range(up_to):
"""Generator to return the sequence of integers from 0 to up_to, exclusive."""
index = 0
def gratuitous_refactor():
nonlocal index
while index < up_to:
yield index
index += 1
yield from gratuitous_refactor()
New 3.6 syntax:
async def func(param1, param2):
do_stuff()
await some_coroutine()
async def read_data(db):
data = await db.fetch('SELECT ...')
async def display_date(loop):
end_time = loop.time() + 5.0
while True:
print(datetime.datetime.now())
if (loop.time() + 1.0) >= end_time:
break
await asyncio.sleep(1)
loop = asyncio.get_event_loop()# Blocking call which returns when the display_date() coroutine is done
loop.run_until_complete(display_date(loop))
loop.close()
Async for¶
Async improvements - 3.6¶
- set comprehension:
{i async for i in agen()}
- list comprehension:
[i async for i in agen()]
- dict comprehension:
{i: i ** 2 async for i in agen()}
- generator expression:
(i ** 2 async for i in agen())
Type hinting¶
Type aliases¶
Other common typings include: Any; Generic, Dict, List, Optional, Mapping, Set, Sequence - expressed as Sequence[int]
T = TypeVar('T', int, float, complex) # T is either or an int, a float or a complex
Vector = Iterable[Tuple[T, T]] #
def inproduct(v: Vector[T]) -> T:
return sum(x*y for x, y in v)
def dilate(v: Vector[T], scale: T) -> Vector[T]:
return ((x * scale, y * scale) for x, y in v)
vec = [] # type: Vector[float]