I’ve been using Python since version 2.2. I do pick up new language constructs like for example with
statement or dictionary/set comprehensions. However, I’ve realized that even though I’m being consistent with PEP-8, for existing constructs I’m using old style, rather than new style (for example except Exception, e
instead of except Exception as e
).
Is there a resource which would have either most current style guide, or even better a list of changes in Python’s coding style?
1
I’d look through the What’s new documentation; whenever new syntax has been introduced it is invariably an improvement over the old.
Each What’s New document (one per minor version, so for 2.5, 2.6, etc.) starts by summarising the Python Enhancement Proposals (PEP) that made it to that release. New syntax, functions and modules that you want to know about are invariably listed as a PEP.
The next documents you can look for is to search for blog posts, presentations and articles about “Idiomatic Python”; these usually try to teach you good, often newer, python style:
-
David Goodger (Python core dev): Code Like a Pythonista: Idiomatic Python
-
Raymond Hettinger (Another core dev): Transforming Code into Beautiful, Idiomatic Python (YouTube video, Slides)
-
Jeff Knupp: Writing Idiomatic Python (e-book), initial blog post that started the e-book.
Last but not least, try answering questions on Stack Overflow on the python tag; any style mistakes you make will be swiftly pointed out to you. It is a very swift path to discovering what better style is availabe. 🙂
5
If you are ready to move to Python 3.x, read Python Cookbook, 3rd Edition, by David Beazley and Brian K. Jones. By solving many practical problems in different areas it showcases a modern style of programming in which takes advantage of the newest language features.