.title-slide # What's New in Python 3.5 .right[Andrey Vlasovskikh] .right[JetBrains] .right[Moscow Python #29] --- .center[![PyCharm](media/pycharm-logo.png)] * I'm [@vlasovskikh](http://twitter.com/vlasovskikh) and [pirx.ru](http://pirx.ru/) * From St.Petersburg, Russia * I work for JetBrains * PyCharm: IDE for Python and Web development * I've been contributing to PEP 484: Type Hints * A part of Python 3.5 --- # Good Docs on What's New * [https://docs.python.org/3/whatsnew/3.5.html](https://docs.python.org/3/whatsnew/3.5.html) * Coroutines with `async` and `await` syntax * New matrix multiplication operator: `a @ b` * Unpacking generalizations: `[*range(4), 5, *(6, 7)]` * Type hints: the `typing` module * Etc. * I'm not going to walk through this document * It's a good read, take a look at it --- # What This Talk is About * Python 2 vs 3 statistics * Pros and cons of new features of 3.5 * Async-await coroutines * Type hints * Should you switch to Python 3? --- .title-slide # Python 2 vs 3 --- # Moscow Python: 2 vs 3 Survey * Which Python version(s) do you regularly use? * Python 2 only * Python 2 and 3 * Python 3 only * What keeps you from switching to Python 3? * No stimulus * Dependencies * Large/legacy code base * Management --- # 2014 Survey * [Survey](http://blog.frite-camembert.net/python-survey-2014.html) by Bruno Cauet * ~6500 participants * Which Python version(s) do you regularly use? * Python 2.7: 81% * Python 3.4: 43% * What keeps you from switching to Python 3? * Dependencies: 59% * Large/legacy code base: 42% * No stimulus: 41% * Management: 10% --- # PyPI Downloads * [Dataset](https://caremad.io/2015/04/a-year-of-pypi-downloads/) for 2015 * Biased: most downloads are from mirrors/CI systems/bots * Overall * Python 2: 74% * Python 3: 6% * For Django * Python 2: 82% * Python 3: 14% --- .title-slide # Pros and Cons of 3.5 Features --- .title-slide # Async-Await --- # Async-Await Coroutines * Python 3.4: sequential syntax for async code * `asyncio` module for event loop * No callback hell thanks to `yield from` syntax * Python 3.5: new syntax for async code * `@asyncio.coroutine` → `async def` coroutine functions * `yield from` → `await` on awaitable objects * `async for` iteration and `async with` context managers --- # Async-Await Example import asyncio .keyword[async] def stream_events(queue): while True: try: response = .keyword[await] aiohttp_request('GET', URL) .keyword[async] for chunk in readiter(response.content): msg = json.loads(chunk.decode('utf-8')) .keyword[await] queue.put(msg) except Exception as e: log.error(e) .keyword[await] asyncio.sleep(RETRY_TIMEOUT) q = asyncio.Queue() loop = asyncio.get_event_loop() loop.run_until_complete(stream_events(q)) --- # Async-Await: Pros * Sequential syntax for async code * [Tornado](http://tornadokevinlee.readthedocs.org/en/latest/guide/coroutines.html) and [Twisted](https://glyph.twistedmatrix.com/2014/05/the-report-of-our-death.html) code could be more readable * New aio\* family of libraries * Coroutine and awaitable as language concepts * Async code looks pythonic now * Fewer reasons to write blocking code * More like Erlang, Scala, Go --- # Async-Await: Cons * Async versions for all blocking I/O libraries * Huge duplication of development efforts * E.g. aiopg vs psycopg2, the Twisted ecosystem, etc. * Django and Flask use blocking I/O * It's a big step to switch to Tornado or aiohttp * No async for CPU-intensive computations in threads * GIL prevents it, only in separate processes * No cross-platform file I/O * Cannot eliminate all blocking operations --- .title-slide # Type Hints --- # Type Hints * Python 3.0: function annotations * Just syntax, no semantics * Python 3.5: standard notation for type hints * Based on function annotations * `typing` module for type system constructs * Third-party static type checkers * No checking happens at runtime --- # Type Hints Example from typing import Any, Iterator def register_namespace(prefix: str, uri: str) -> None: ... def iselement(element: Any) -> bool: ... class Element: def itertext(self) -> Iterator[str]: ... .center[![](media/type-inference.png)] --- # Type Hints: Pros * Tools for linting, code completion, refactoring * Type information is crucial for static code analysis * More like TypeScript, Erlang * Better documentation * More compact and than lengthy native language descriptions * It's enough to annotate APIs * Type checkers can infer types --- # Type Hints: Cons * May look like static typing * More code to write, no apparent benefits for small projects * Tools don't support Python 3.5 yet * PyCharm 5 and Mypy are in beta, Pylint in 2016 * Very few libraries with type hints * Third-party annotations via stub files duplicate API definitions --- .title-slide # Should You Switch to Python 3? --- .title-slide # Yes ;) --- # It's Up to You to Decide * How to answer the reasons not to switch * No stimulus * New features, better performance, plenty of bug fixes * Dependencies * [Lots of ported popular libraries](https://python3wos.appspot.com) * Large/legacy code base * [Porting to Python 3 guide](http://python3porting.com) * Management --- # Thank You! * Feel free to ask any questions * [@vlasovskikh](http://twitter.com/vlasovskikh) and [pirx.ru](http://pirx.ru/) * SpbPy * St. Petersburg Python Meetup * [http://spbpy.ru/](http://spbpy.ru) * 2015-10-22 20:00