What's New in Pythons 3.5 and 3.6?
Viacheslav Kakovskyi
Austin Python Meetup 2017
my favourite features
Me!
@kakovskyi
Backend Software Technical Lead
Python 2 and Twisted → Python 3 and asyncio
Share knowledge at Python events
● PyCon Ukraine 2016
● PyCon Poland 2016
● KyivPy 2015, WebCamp Ukraine 2016
2
Agenda
1. Syntax changes
2. Library modules
3. Built-in features
4. CPython improvements
5. Standard library improvements
6. Summary
7. Further reading
3
Who uses Python 3 in production?
4
Who uses Python 3 in production?
5
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))
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}
Syntax changes
PEP 515 - underscores in numeric literals
8
Python 3.6
>>> 100_500_000
100500000
>>> 0x_DE_AD_BE_AF
3735928495
>>> '{:_}'1.format(100500000)
100_500_000
>>> '{:_x}'1.format(3735928495)
0x_DE_AD_BE_AF
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()]
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)
Built-in features
PEP-461 - percent formatting support for bytes and bytearray
11
Python 3.5
>>> b'Hello, %b' % b'Austin!'
b'Hello, Austin!'
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
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))
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}
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
● 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
● 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
Summary
● Use Python 3, asyncio and aiohttp for networking
applications
18
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
20
@kakovskyi
viach.kakovskyi@gmail.com
Questions?
Senior Platform Developers needed
tiny.cc/atlassian_atx
21

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

  • 1.
    What's New inPythons 3.5 and 3.6? Viacheslav Kakovskyi Austin Python Meetup 2017 my favourite features
  • 2.
    Me! @kakovskyi Backend Software TechnicalLead Python 2 and Twisted → Python 3 and asyncio Share knowledge at Python events ● PyCon Ukraine 2016 ● PyCon Poland 2016 ● KyivPy 2015, WebCamp Ukraine 2016 2
  • 3.
    Agenda 1. Syntax changes 2.Library modules 3. Built-in features 4. CPython improvements 5. Standard library improvements 6. Summary 7. Further reading 3
  • 4.
    Who uses Python3 in production? 4
  • 5.
    Who uses Python3 in production? 5
  • 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}
  • 8.
    Syntax changes PEP 515- underscores in numeric literals 8 Python 3.6 >>> 100_500_000 100500000 >>> 0x_DE_AD_BE_AF 3735928495 >>> '{:_}'1.format(100500000) 100_500_000 >>> '{:_x}'1.format(3735928495) 0x_DE_AD_BE_AF
  • 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 israised 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 Eliminationof 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 andordered 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 customizationof 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 isimplemented 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 receivednew 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 Python3, 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
  • 20.
  • 21.
    Senior Platform Developersneeded tiny.cc/atlassian_atx 21