6. ② Iterators, Iterators Everywhere
Consume about 1,5 GB of memory with Python
2 !
range vs xrange, keys vs iterkeys, values vs
itervalues, items vs iteritems
zip, map, filter
>>> for _ in range(50000000):
... the_answer = 42
13. ⑤ Chained Exceptions
>>> try:
... connection = do_connect()
... # do stuff
... finally:
... connection.close()
Traceback (most recent call last):
File "code.py", line 5, in <module>
connection.close()
NameError: name 'connection' is not defined
14. ⑤ Chained Exceptions
Traceback (most recent call last):
File "plop.py", line 2, in <module>
connection = do_connect()
ConnectionRefusedError: [Errno 111]
Connection refused
During handling of the above exception,
another exception occurred:
Traceback (most recent call last):
File "plop.py", line 5, in <module>
connection.close()
NameError: name 'connection' is not defined
18. ⑦ Reworked Stdlib Names
Which module ? urllib, urllib2, urlparse ?
– Parsing an url : urlparse
– Quoting an URL : urllib
– Do a requests : urllib2
No more cPickle, cProfile, cStringIO
22. ⑨ asyncio and async/await
>>> reader,writer = await asyncio.open_connection(
... "www.python.org", 80)
... writer.write(b'GET / […]')
... async for line in reader:
... # do something with the line
23. ⑩ Bonus
>>> 1 / 2
0.5
>>> for x in iterable:
... yield x
>>> yield from iterable
24. ⑩ Bonus
Tab-completion in interpreter
>>> class Children(Parent):
... def method(self):
... super().method()