py350¶
PEP 448¶
In [1]:
print(*[1], *[2], 3, *[4, 5])
1 2 3 4 5
In [2]:
def fn(a, b, c, d):
print(a, b, c, d)
fn(**{'a': 1, 'c': 3}, **{'b': 2, 'd': 4})
1 2 3 4
In [3]:
*range(4), 4
Out[3]:
(0, 1, 2, 3, 4)
In [4]:
[*range(4), 4]
Out[4]:
[0, 1, 2, 3, 4]
In [5]:
{*range(4), 4, *(5, 6, 7)}
Out[5]:
{0, 1, 2, 3, 4, 5, 6, 7}
In [6]:
{'x': 1, **{'y': 2}}
Out[6]:
{'x': 1, 'y': 2}
PEP 465¶
In [1]:
import numpy
x = numpy.ones(3)
x
Out[1]:
array([ 1., 1., 1.])
In [2]:
m = numpy.eye(3)
m
Out[2]:
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
In [3]:
x @ m
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-d5605b4856c5> in <module>()
----> 1 x @ m
TypeError: unsupported operand type(s) for @: 'numpy.ndarray' and 'numpy.ndarray'
In [5]:
numpy.version.version
Out[5]:
'1.9.3'
PEP 484¶
In [6]:
def greeting(name: str) -> str:
return 'Hello ' + name
greeting(1)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-730f7b8fc003> in <module>()
1 def greeting(name: str) -> str:
2 return 'Hello ' + name
----> 3 greeting(1)
<ipython-input-6-730f7b8fc003> in greeting(name)
1 def greeting(name: str) -> str:
----> 2 return 'Hello ' + name
3 greeting(1)
TypeError: Can't convert 'int' object to str implicitly
In [ ]:
In [9]:
import os
for entry in os.scandir('.'):
if not entry.name.startswith('.') and entry.is_file():
print(entry.name)
Untitled.ipynb
PEP 492¶
In [11]:
import asyncio
async def coro(name, lock):
print('coro {}: waiting for lock'.format(name))
async with lock:
print('coro {}: holding the lock'.format(name))
await asyncio.sleep(1)
print('coro {}: releasing the lock'.format(name))
loop = asyncio.get_event_loop()
lock = asyncio.Lock()
coros = asyncio.gather(coro(1, lock), coro(2, lock))
try:
loop.run_until_complete(coros)
finally:
loop.close()
coro 2: waiting for lock
coro 2: holding the lock
coro 1: waiting for lock
coro 2: releasing the lock
coro 1: holding the lock
coro 1: releasing the lock
PEP 475 ?¶
In [18]:
import io
io.open
Out[18]:
<function io.open>
In [19]:
open
Out[19]:
<function io.open>
PEP 485¶
In [20]:
import math
a = 5.0
b = 4.99998
In [21]:
math.isclose(a, b, rel_tol=1e-5)
Out[21]:
True
In [22]:
math.isclose(a, b, rel_tol=1e-6)
Out[22]:
False
In [23]:
math.isclose(a, b, abs_tol=0.00003)
Out[23]:
True
In [24]:
math.isclose(a, b, abs_tol=0.00001)
Out[24]:
False
In [ ]:
enum¶
In [26]:
import enum
Animal = enum.Enum('Animal', 'cat dog', start=10)
Animal
Out[26]:
<enum 'Animal'>
In [29]:
Animal.cat
Out[29]:
<Animal.cat: 10>
In [30]:
Animal.dog
Out[30]:
<Animal.dog: 11>
heap que¶
http://d.hatena.ne.jp/dwarfjay/20140418/1397825281
In [32]:
import heapq
a = ['9', '777', '55555']
b = ['88', '6666']
In [33]:
list(heapq.merge(a, b, key=len))
Out[33]:
['9', '88', '777', '6666', '55555']
In [34]:
list(heapq.merge(reversed(a), reversed(b), key=len, reverse=True))
Out[34]:
['55555', '6666', '777', '88', '9']
logging¶
In [35]:
import logging
try:
1/0
except ZeroDivisionError as ex:
logging.error('exception', exc_info=ex)
ERROR:root:exception
Traceback (most recent call last):
File "<ipython-input-35-88ff96dbbd0a>", line 3, in <module>
1/0
ZeroDivisionError: division by zero
math¶
In [39]:
import math
import fractions
In [43]:
math.gcd(4, 6)
Out[43]:
2
In [44]:
math.nan
Out[44]:
nan
In [45]:
type(math.nan)
Out[45]:
float
In [47]:
math.inf
Out[47]:
inf
In [48]:
type(math.inf)
Out[48]:
float
In [49]:
float('inf')
Out[49]:
inf
In [50]:
float('nan')
Out[50]:
nan
Changes in Python behavior¶
In [51]:
print(1 for x in [1], *[2])
File "<ipython-input-51-4e05ab3a9316>", line 1
print(1 for x in [1], *[2])
^
SyntaxError: Generator expression must be parenthesized if not sole argument
In [53]:
# 3.4.3
# print(1 for x in [1], *[2])
# <generator object <genexpr> at 0x103e0ffc0> 2