Python Forum
assignment: not an operator nor expression, but x=y=z=3 works fine?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
assignment: not an operator nor expression, but x=y=z=3 works fine?
#1
Hi,
When I read the Python reference (Assignment statements), the documenation doesn't describe an repeated assingment syntax (as far as my BNF knowledge brings me).

Still, if I code a=b=c=d=3, the variables a b c and d get the value 3

If they were interpreted from left to right (like C#) and the assignment were an expression (operator) with the assigned value as the result, it would be ok, but the same documentation doesn't mention this. Also, the assignment is not found among the operators (nor the operator precedence list)

Although it works and doesn't give me any problems, I'm curious about how this relates to the reference: am I overlooking something? (if I want to use the reference to work out about details, I should know how to interpret that reference, and this doesn't seem the case right now)

Kind regards, and thanks in advance,
Jef Daels
Reply
#2
You can read detailed answer from StackOverflow: Python Multiple Assignment Statements In One Line

Somewhat loosely related (from documentation you referred):

Quote:Although the definition of assignment implies that overlaps between the left-hand side and the right-hand side are ‘simultaneous’ (for example a, b = b, a swaps two variables), overlaps within the collection of assigned-to variables occur left-to-right, sometimes resulting in confusion. For instance, the following program prints [0, 2]:

x = [0, 1]
i = 0
i, x[i] = 1, 2         # i is updated, then x[i] is updated
print(x)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to fine tune cutecharts? Gigux 1 1,205 Jan-28-2022, 05:41 PM
Last Post: Larz60+
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 1,929 Dec-18-2021, 02:38 AM
Last Post: knight2000
  Python Regular expression, small sample works but not on file Acernz 5 2,858 Jun-09-2021, 08:27 PM
Last Post: bowlofred
  Multimode imports fine as script but not after compiling into exe chesschaser 0 2,371 Aug-13-2020, 01:28 PM
Last Post: chesschaser
  New to Python, How does this lambda expression works? Joshh_33 2 1,997 Mar-26-2020, 03:32 PM
Last Post: Joshh_33
  Pass results of expression to another expression cmdr_eggplant 2 2,238 Mar-26-2020, 06:59 AM
Last Post: ndc85430
  Pyautogui script runs fine if split into two parts together it does not Bmart6969 1 2,499 Oct-07-2019, 10:53 PM
Last Post: Bmart6969
  Assignment operator DavidTheGrockle 7 3,363 Aug-11-2019, 07:16 AM
Last Post: ThomasL
  How many variables/values are limited to single assignment operator? Prabakaran141 1 2,000 Sep-06-2018, 03:32 PM
Last Post: Larz60+
  assignment embedded in an expression Skaperen 11 6,336 Nov-25-2017, 03:37 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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