Python Coding Style – Space Between Negative Sign and Variable Name

coding-stylepython

I tried doing a Google search, as well as searching this Stack Exchange site but could not find a question relating directly to this.

The PEP 8 — Style Guide for Python Code has lots of good style recommendations, however I don't think they mention anything about whether a space between a minus or negative sign (-) and a variable name would be more or less readable.

Question

It's a (very) minor issue, but which is considered more readable?

  • false_positive = -true_positive
  • false_positive = - true_positive

Motivation

I ask not to be trivial, but because in my past math courses I often find myself not noticing the negative sign in important equations, such as the equation for computing entropy:

entropy formula

I often don't notice the - sign before the summation, so making it stand out would be desirable.

Best Answer

I couldn't find anything about it in the Python style guide, as you said, but searching for "unary operator spacing" brought more hits from various languages, such as this for Javascript:

No space should separate a unary operator and its operand except when the operator is a word such as typeof.

Or this for C:

Don't use spaces around unary operators, except sizeof and casts

In the Linux kernel:

Use one space around (on each side of) most binary and ternary operators, [...] but no space after unary operators:

Note that a writing style for code, which is usually written with a fixed-width font and mostly Latin characters, is quite different than that for equations, where you can find both latin and greek letters, topological constructs such as fractions or those marks surrounding that epsilon you have there, and other things that make them a whole lot more complex than code.