Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Что нового в Python 3.5

Что нового в Python 3.5

Когда полезны аннотации типов? Станет ли асинхронное программирование обычной практикой с новыми async-await? Устроим обсуждение этих и других новинок Python 3.5

Moscow Python Meetup

October 08, 2015
Tweet

More Decks by Moscow Python Meetup

Other Decks in Programming

Transcript

  1. • I'm @vlasovskikh and 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
  2. Good Docs on What's New • 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
  3. 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?
  4. 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
  5. 2014 Survey • Survey 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%
  6. 2014 Survey • Dataset 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%
  7. 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
  8. Async-Await Example import asyncio async def stream_events(queue): while True: try:

    response = await aiohttp_request('GET', URL) async for chunk in readiter(response.content): msg = json.loads(chunk.decode('utf-8')) await queue.put(msg) except Exception as e: log.error(e) await asyncio.sleep(RETRY_TIMEOUT) q = asyncio.Queue() loop = asyncio.get_event_loop() loop.run_until_complete(stream_events(q))
  9. Async-Await: Pros • Sequential syntax for async code ◦ Tornado

    and Twisted 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
  10. 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
  11. 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
  12. 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]: ...
  13. 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
  14. 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
  15. 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 ◦ Large/legacy code base ▪ Porting to Python 3 guide ◦ Management
  16. Thank You! • Feel free to ask any questions ◦

    @vlasovskikh and pirx.ru • SpbPy ◦ St. Petersburg Python Meetup ◦ http://spbpy.ru/ ◦ 2015-10-22 20:00