Когда полезны аннотации типов? Станет ли асинхронное программирование обычной практикой с новыми async-await? Устроим обсуждение этих и других новинок Python 3.5
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
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
• 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%
◦ 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
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
◦ 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
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 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
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
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