Spacing conventions

Thomas Jollans tjol at tjol.eu
Wed Sep 27 04:10:58 EDT 2017


On 27/09/17 09:50, Bill wrote:
> Ever since I download the MyCharm IDE a few days ago, I've been
> noticing all sort of "spacing  conventions (from PEP) that are
> suggested.  How do folks regard these in general?

PEP 8 (https://www.python.org/dev/peps/pep-0008), the officially
recommended style guide for Python code, is generally well regarded and
widely followed. Having a consistent style, whatever it is, makes your
code more readable. You can of course have your own house style, but
using the same style as most other Pythonistas for new code helps other
people read your code. I recommend following PEP 8 as much as possible.

>
> For instance,  the conventions suggest that
>
> if x>y :
>      pass
>
> should be written
> if x > y:
>     pass
>
> Personally, I like seeing a space before the colon (:).   And then in
>
> my_list = [ i for i in range(0, 10) ]
> it complains about my extra space inside of the brackets.

I think you'll find that spaces before colons are exceedingly rare in
Python code. Spaces inside brackets, especially in list comprehensions,
may be more common.

Personally I've found that my preferred tool, the Anaconda plugin for
Sublime Text, sometime gets PEP 8 operator spacing wrong, and complains
operators without spaces even where PEP8 explicitly recommends not using
spaces. Read PEP 8, follow it if possible, but use your best judgement.
Readability is important, blindly following your IDE's advice not so much.

-- Thomas

>
> If you are teaching beginning students, do you expect them to try to
> follow these sorts of conventions?  Is it perfectly fine to let
> "taste" guide you (I'm just trying to get a feel for the philosophy
> here)?   I also notice "break" and exception handling is used much
> more in Python than in C++, for instance.  I was taught "break" and
> "continue" led to "unstructured code"--but that was a while back.  I
> can still see their use  causing potential trouble in (really-long)
> real-world code.
>
> Bill
>
>
>




More information about the Python-list mailing list