Python Forum
Python rule about the space character surrounding the equal sign
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python rule about the space character surrounding the equal sign
#1
* 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?
Linux Mint Cinnamon 21.3 - Python 3.10.12 - Autokey-gtk 0.96.0 as of 2024-01-13.
Reply
#2
did you try it?
>>> a=3
>>> a
3
>>>
Reply
#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).
Linux Mint Cinnamon 21.3 - Python 3.10.12 - Autokey-gtk 0.96.0 as of 2024-01-13.
Reply
#4
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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.
Larz60+ and buran like this post
Reply
#6
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
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.
Larz60+ likes this post
Reply
#8
(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.
Linux Mint Cinnamon 21.3 - Python 3.10.12 - Autokey-gtk 0.96.0 as of 2024-01-13.
Reply
#9
(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"
Reply
#10
(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.
ineuw likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Does @ at sign used for tother than decorators? ggpython000 1 549 Oct-12-2023, 09:08 AM
Last Post: buran
  How do I handle escape character in parameter arguments in Python? JKR 6 1,177 Sep-12-2023, 03:00 AM
Last Post: Apoed2023
  Removing Space between variable and string in Python coder_sw99 6 6,310 Aug-23-2022, 01:15 PM
Last Post: louries
  is there equal syntax to "dir /s /b" kucingkembar 2 1,003 Aug-16-2022, 08:26 AM
Last Post: kucingkembar
  Need to sign JWT token with JWK key stucoder 1 1,695 Feb-21-2022, 09:04 AM
Last Post: stucoder
  Can a variable equal 2 things? Extra 4 1,511 Jan-18-2022, 09:21 PM
Last Post: Extra
  i making a terminal sign up website thing Kenrichppython 1 1,711 Nov-04-2021, 03:57 AM
Last Post: bowlofred
  enter string into list according to a known rule korenron 10 3,307 Aug-12-2021, 02:19 PM
Last Post: korenron
  [solved] unexpected character after line continuation character paul18fr 4 3,433 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  python error: bad character range \|-t at position 12 faustineaiden 0 3,702 May-28-2021, 09:38 AM
Last Post: faustineaiden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020