The Zen of Python says:
There should be one— and preferably only one —obvious way to do it.
Python already has a style guide in PEP 8. More importantly, from a practical standpoint, Python has automated third-party tools for checking and enforcing PEP 8 guidelines:
pep8
: Python style guide checkerautopep8
: A tool that automatically formats Python code to conform to the PEP 8 style guideI say “more importantly” because these tools are what make the style guide alive. If a Python project of any non-trivial size is PEP 8-compliant, you can bet it’s because the project contributors automated the process of enforcing compliance using these or similar tools. (Certainly, some parts of PEP 8 are hard to automate. But those parts that can be automated have been, to the benefit of Python programmers everywhere.)
Having a style guide provides benefits that I think are well-understood. Additionally having a standardized style that can be applied automatically makes whatever benefits of the style guide that much more prevalent in the greater community and actually effective.
As many of you may already know, the Go-lang folks have such a feature for their language (though they call it formatting as opposed to styling). They note:
Gofmt is a tool that automatically formats Go source code.
Gofmt’d code is:
- easier to write: never worry about minor formatting concerns while hacking away,
- easier to read: when all code looks the same you need not mentally convert others’ formatting style into something you can understand.
- easier to maintain: mechanical changes to the source don’t cause unrelated changes to the file’s formatting; diffs show only the real changes.
- uncontroversial: never have a debate about spacing or brace position ever again!
Of course, we don’t worry about brace positions, but these benefits of a standardized style carry over into any language. :)
So, in the spirit of The Zen of Python, and taking a cue from Go-lang’s
gofmt
, I propose we promote standardized auto-styling to a first-class language feature.UI-wise, I’m not sure how exactly we’d present this to the user. Perhaps something like:
python -m style script.py
Don’t read too much into this UI suggestion, as I’m really not sure how it would be implemented. The point is to expose some auto-styler as a standard language tool—i.e. something like
autopep8
, but as a built-in utility.On running a command like that,
script.py
would automatically be updated to conform to the standard style we want to promote. This is whatgofmt
does for Go code.Promoting an auto-styler to be a first-class language feature builds on the foundation laid in PEP 8 and in tools like
pep8
andautopep8
. It helps reduce code style to more of a non-issue for Python projects than it already is.Side Note 1: There was a previous python-ideas discussion about introducing standardized formatting here, but it was totally derailed by the OP’s suggestion that the formatter introduce block delimiters. I am proposing no such thing here.
Side Note 2: I found these additional comments about gofmt interesting:
Gofmt also enables gofix, which can make arbitrarily complex source transformations. Gofix was an invaluable tool during the early days when we regularly made breaking changes to the language and libraries.
Gofix gives us the ability to fix mistakes or completely rethink package APIs without worrying about the cost of converting existing code.
Python has its own analog to
gofix
in2to3
, though that was built for a specific migration rather than as a regular language feature. I wonder if having agofmt
analog in Python would similarly facilitate future migrations, though I doubt we’ll ever have a big as migration as the 2-to-3 one.
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/