austin python meetup 2017: what's new in pythons 3.5 and 3.6?

21
What's New in Pythons 3.5 and 3.6? Viacheslav Kakovskyi Austin Python Meetup 2017 my favourite features

Upload: viacheslav-kakovskyi

Post on 23-Jan-2017

306 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?

What's New in Pythons 3.5 and 3.6?

Viacheslav Kakovskyi Austin Python Meetup 2017

my favourite features

Page 2: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?

Me!@kakovskyi

Backend Software Technical LeadPython 2 and Twisted → Python 3 and asyncioShare knowledge at Python events● PyCon Ukraine 2016● PyCon Poland 2016● KyivPy 2015, WebCamp Ukraine 2016

2

Page 3: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?

Agenda1. Syntax changes2. Library modules3. Built-in features4. CPython improvements5. Standard library improvements6. Summary7. Further reading

3

Page 4: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?

Who uses Python 3 in production?

4

Page 5: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?

Who uses Python 3 in production?

5

Page 6: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.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))

Page 7: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?

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}

Page 8: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?

Syntax changes

PEP 515 - underscores in numeric literals

8

Python 3.6

>>> 100_500_000100500000>>> 0x_DE_AD_BE_AF3735928495

>>> '{:_}'1.format(100500000)100_500_000>>> '{:_x}'1.format(3735928495)0x_DE_AD_BE_AF

Page 9: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?

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()]

Page 10: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?

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)

Page 11: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?

Built-in features

PEP-461 - percent formatting support for bytes and bytearray

11

Python 3.5

>>> b'Hello, %b' % b'Austin!'b'Hello, Austin!'

Page 12: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?

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

Page 13: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?

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))

Page 14: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?

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}

Page 15: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?

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

Page 16: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?

● 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

Page 17: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?

● 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

Page 18: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?

Summary

● Use Python 3, asyncio and aiohttp for networking applications

18

Page 19: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?

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

Page 20: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?

20

@[email protected]

Questions?

Page 21: Austin Python Meetup 2017: What's New in Pythons 3.5 and 3.6?

Senior Platform Developers needed

tiny.cc/atlassian_atx21