SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?
In the talk, the changes introduced in Python 3.5 and Python 3.6 are covered. Primary based on standard documentation, but only my favourite ones are included:
In the talk, the changes introduced in Python 3.5 and Python 3.6 are covered. Primary based on standard documentation, but only my favourite ones are included:
6.
Syntax changes
PEP 492 - coroutines with async and await syntax
● async expression used to define coroutine functions
● await is used to suspend coroutine execution until result is
available
● implement object.__await__(self) to make your object
awaitable
6
Python 3.5
async def http_handler():
users = await get_users_from_db()
return '<h1>Online users {}</h1>'.format(len(users))
7.
Syntax changes
PEP 448 - additional unpacking generalizations
● * expression used as iterable unpacking operator
● and ** as dictionary unpacking operator
7
Python 3.5
>>> print(*[1], *{2}, 3)
1 2 3
>>> print({'x': 4, **{'y': 5}})
{'x': 4, 'y': 5}
9.
Syntax changes
PEP 530 - asynchronous comprehensions
● Now it's possible to use async for in list, set, dict
comprehensions and generator expressions
● await and can be used inside the expressions
9
Python 3.6
result = [await fun() for fun in funcs if await condition()]
10.
Library modules
typing - support of type hints for static code analysis only for
functions
PEP 526 - add support of type annotation for variables (Py3.6)
● Any, Awaitable Callable, Collection, Coroutine,
● Dict, FrozenSet, Generator, Iterable, Iterator
● List, NewType, Set, Tuple
10
Python 3.5
def greeting(name: str) -> str:
return 'Hello, {}'.format(name)
11.
Built-in features
PEP-461 - percent formatting support for bytes and bytearray
11
Python 3.5
>>> b'Hello, %b' % b'Austin!'
b'Hello, Austin!'
12.
Built-in features
RecursionError is raised when max recursion depth is reached
12
Python 3.5
def recursion(level):
# do something awesome
return recursion(level+1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in a
File "<stdin>", line 2, in a
File "<stdin>", line 2, in a
[Previous line repeated 995 more times]
RecursionError: maximum recursion depth exceeded
13.
CPython improvements
PEP-488 Elimination of PYO files
● .pyo files are never used
● .pyc files are used with explicit declaration of optimization
level in filename
13
Python 3.5
'{name}.{cache_tag}.opt-{optimization}.pyc'.format(name=module_name,
cache_tag=sys.implementation.cache_tag,
optimization=str(sys.flags.optimize))
14.
CPython improvements
Compact and ordered dict improvement
● Memory usage is 20% less compared to Python 3.5
● dict preserves order of insertion now
14
Python 3.6
>>> d = {}
>>> d['a'] = 1
>>> d['b'] = 2
>>> d['c'] = 3
>>> d
{'a': 1, 'b': 2, 'c': 3}
15.
PEP-487 Simpler customization of class creation
● no metaclasses, I promise you
● __init_subclass__ method will be called on base class when
a subclass is created
CPython improvements
15
Python 3.6
16.
● colection.OrderedDict is implemented in C and just faster
● os.scandir() provides faster way for directory traversal
● functools.lru_cache() has been reimplemented in C
● traceback is more handy for devs and faster too
Standard library improvements
16
Python 3.5
17.
● asyncio received new features and considered as stable
● typing has been improved and isn't in provisional state
● tracemalloc provides better output for memory allocation
errors
● pathlib has been improved and system path protocol is
implemented
Standard library improvements
17
Python 3.6
18.
Summary
● Use Python 3, asyncio and aiohttp for networking
applications
18
19.
Further reading
● @kakovskyi
○ Maintaining a high load Python project for newcomers
○ Maintaining a high load Python project: typical mistakes
○ Instant messenger with Python. Back-end development
○ How to easily find the optimal solution without exhaustive search
using Genetic Algorithms
● @bmwant
○ Asyncio-stack for web development
○ PEP8 is not enough
○ PyInvoke is your replacement for Makefile
19