Python Forum

Full Version: Python rule about the space character surrounding the equal sign
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
* In Linux Mint Cinnamon, I am using Autokey 0.96.0 which uses Python 3+.
* I was told that in Python, spaces must surround the = sign in variable assignment.
* This is different from the bash files I create. The bash file fails, if spaces surround the = sign.

I couldn't find an answer, so I came to ask the sages of Python.

What is the rule about spacing regarding variable assignments?
did you try it?
>>> a=3
>>> a
3
>>>

I agree with you 100%, but I am need a clear answer in writing. I intend to fight entrenched ignorance or coding habits. This may end up in the PSC. (Python Supreme Court).
It's a matter of style, not rule that will brake your code. Check PEP8:
Quote:Always surround these binary operators with a single space on either side: assignment (=), augmented assignment (+=, -= etc.), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), Booleans (and, or, not).
I don't know what/how you plan to fight it, but I can tell you with 100% certainly it will be futile excersise.
Remark that most people follow PEP8 but PEP8 is actually a coding style for the contributors of Python's standard library. Other teams or individual programmers are free to use their own coding conventions.

Following PEP8 eases communication with other Python programmers. It also contents some automated tools that check the quality of your code.
(Jul-12-2023, 06:39 AM)Gribouillis Wrote: [ -> ]Remark that most people follow PEP8 but PEP8 is actually a coding style for the contributors of Python's standard library.
To add to @Gribouillis remark - read at least the introduction and first section of PEP8. Anyway it's a good idea to familiarize yourself with the whole document, regardless if you decide to follow it or not.
You could also reformat the code automatically with Black for example. Then you wouldn't worry again about the correct spacing between operators. You still need to read PEP8's advice on the good naming conventions for class, variables and functions.
(Jul-12-2023, 07:06 AM)Gribouillis Wrote: [ -> ]You could also reformat the code automatically with Black for example. Then you wouldn't worry again about the correct spacing between operators. You still need to read PEP8's advice on the good naming conventions for class, variables and functions.

Thanks for the suggestion. "Black" is excellent, but had to revert the double quotes to single. Everything is in single quotes in the context of my use.
(Aug-22-2023, 08:06 PM)ineuw Wrote: [ -> ]Everything is in single quotes in the context of my use.
Unlike other languages like C, Python makes no difference between single quotes or double quotes for literal strings. 'hello' and "hello" represent the same string, as well as 'A' and "A"
(Aug-22-2023, 08:06 PM)ineuw Wrote: [ -> ]Thanks for the suggestion. "Black" is excellent, but had to revert the double quotes to single. Everything is in single quotes in the context of my use.

It depends on your Keyboard layout. On a German layout, the double quote is Shift + 2 and the single quote is next to the Return key Shift + #.

You could try this: https://github.com/psf/black/issues/118#...-393298377
Quote:Resolution: 18.6b0 has --skip-string-normalization, or -S for short. This allows existing projects to adopt Black with less friction and works for all alternative string quotes policies.

Black still defaults to and recommends normalizing string quotes to double quotes everywhere as we believe this is a better default than single quotes, and is enforceable unlike the "single quotes for data, double quotes for human-readable strings" policy.

I hope this resolves to your satisfaction what's been the most controversial issue in Black's history.
Pages: 1 2