BDFL Python 3 retrospective by Guido van Rossum


2018/03/05 发布于 技术 分类


1. Python 3 retrospective What’s working, what failed, and what’s still a work in progress as the language evolves Guido van Rossum Principal Software Engineer, Dropbox 1/22/2018 PyCascades, Vancouver, Canada
6. Evolving a language • A language is the bedrock for other systems – must always be there • Evolving a programming language is like rebuilding a freeway • There are lane closings, but things will get better (until the next big project :-) • Compatibility is king • Nobody likes it when their code breaks (but everyone wants new features :-) • Old features are deprecated, new ones are added • Eventually the deprecated parts can be deleted (but it takes forever :-) • New features must be designed with this process in mind • Don't paint yourself into a corner – expect the need for unexpected changes • Allow room for future additions – you won’t guess all your users’ needs • Design conservatively – you can’t take back design mistakes easily
7. Python 2 was getting old • Lists of "Python warts" were circulating • e.g unicode mess; long vs. int; two class systems; relative imports; int division vs. float division; comparisons; nonlocal variables; ... • Not all warts could be fixed without breaking compatibility • esp. the unicode mess • What were we to do? • We chose to break backwards compatibility, one time only
8. Risk management • Don't break everything, only acknowledged warts • Have a community process for deciding what to change • Don't fall in the Perl 6 trap • Don't reimplement the interpreter from scratch • Keep the language recognizable • Release a few trial versions • Plan end of life for Python 2
9. What went wrong • We underrated Python's popularity • We underestimated the importance of 3rd party packages (PyPI) • The proposed workflow for user code migration was clumsy • We didn't build any runtime compatibility features • We didn't know how to create fully-automated conversion tools • We didn't make enough compatibility allowances (e.g. u"...") • We did a few things half-heartedly (esp. the stdlib cleanup)
10. What went right • Python is still wildly popular, and getting more so • Python 3 is thriving • We avoided a community split • By supporting 2.7 for 9+ year (end of life is finally coming in 2020) • The community came through with tools, guides and more • Almost all packages are ported now • (Wish that was the case for proprietary code though) • Python 3 really is much better than Python 2
12. Incentives to move to Python 3 • Performance is better; and getting better every release • Rational unicode behavior (it’s still so much better than Python 2!) • The nonlocalkeyword; argument-less super() calls • The asyncio library and the async def/aw ait keywords • F-strings: print(f"The sum of{a} and {b} is {a+ b}") • Native type annotations • Dictionaries preserve order • Dataclasses (class decorator auto-generate dunder methods; 3.7) • UTF-8 mode (default to UTF-8 in POSIX locale; 3.7)
13. Ho-hum features • Numeric tower didn’t turn out very useful; PEP 484 declined to use • WSGI didn’t evolve enough (or at all, really) • New .format() method is too verbose (f-strings to the rescue!) • The C-level buffer API was probably too complicated • Unicode is still hard... • What’s on the filesystem? UTF-8 (Mac), UTF-16 (Windows), or bytes (Linux)? • Default encodings are conservative, so UnicodeError may still happen • Some modules use str instead of bytes – e.g. xml, html, json, email • Individual byte values are ints – causes pain for ported code
14. If you're still on Python 2 • Don't despair! There are tools and books and talks to help • ...But now is the time to start planning your migration • Start with a foundation of good test coverage • Make an inventory of 3rd party dependencies • Port leaf packages to “straddling” mode (compatible with 2 and 3) • Use 'six' module to deal with library differences • See also • Do trial ports of smaller packages • Don't redesign your app at the same time • Cleanup is fine
15. The future • Python 3.7 will be awesome (trust me) • Python 4.0 (when it happens) will be a much smoother transition • Also, flying cars
16. 2.8
17. +25