9

While shortening my code I was cutting down a few variable declarations onto one line-

##For example- going from-
Var1 =15
Var2 = 26
Var3 = 922

##To-
Var1, Var2, Var3 = 15, 26, 922

However, when I tried doing the same thing to this code-

User_Input += Master_Key[Input_ref]
Key += Master_Key[Key_ref]
Key2 += Master_Key[Key_2_Ref]

##Which looks like-
User_Input, Key, Key2 += Master_Key[Input_Ref], Master_Key[Key_Ref], Master_Key[Key_2_Ref]

This throws the error

SyntaxError: illegal expression for augmented assignment

I have read the relevant Python documentation, but I still can't find a way to shorten this particular bit of code.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Theo Pearson-Bray
  • 775
  • 1
  • 13
  • 32
  • 7
    Why does it need shortening? It's more straightforward and readable in the original form. – Holloway Jan 17 '15 at 18:06
  • It doesn't necessarily, I was just trying to see if there was a simpler way to do it. When each line is basically adding a character to a string.For further information, this bit of the program is generating a random Input, Key and another Key for testing an encryption program. It does this using a for loop which picks a random number and references it against a string. – Theo Pearson-Bray Jan 17 '15 at 18:24
  • ' def Random_Test_Algorithm(Input_Length, Repeat_times): for i in range(Repeat_times): User_Input, Key, Key2 = "", "", "" for i in range(Input_Length): Input_ref, Key_ref, Key_2_Ref = random.randint(0, len(Master_Key)-1), random.randint(0, (len(Master_Key)-1)), random.randint(0, (len(Master_Key)-1)) User_Input += Master_Key[Input_ref] Key += Master_Key[Key_ref] Key2 += Master_Key[Key_2_Ref] ' – Theo Pearson-Bray Jan 17 '15 at 18:24
  • How do I format that as code? It says ' code ' which didn't do anything – Theo Pearson-Bray Jan 17 '15 at 18:26
  • You can't format as blocks of code in comments. Only inline snippets by surrouding the text with ` characters (backticks not apostrophes). Add any relevant code to the question. – Holloway Jan 17 '15 at 18:45
  • Does this answer your question? [Python augmenting multiple variables inline](https://stackoverflow.com/questions/18132687/python-augmenting-multiple-variables-inline) – Georgy Aug 14 '20 at 16:38

1 Answers1

11

No, you cannot. You cannot use augmented assignment together with multiple targets.

You can see this in the Augmented assignment statements section you linked to:

augmented_assignment_stmt ::=  augtarget augop (expression_list | yield_expression)
augtarget                 ::=  identifier | attributeref | subscription | slicing

The augtarget rule only allows for one target. Compare this with the Assignment statements rules:

assignment_stmt ::=  (target_list "=")+ (expression_list | yield_expression)
target_list     ::=  target ("," target)* [","]
target          ::=  identifier
                     | "(" target_list ")"
                     | "[" target_list "]"
                     | attributeref
                     | subscription
                     | slicing

where you have a target_list rule to assign to.

I'd not try and shorten this at all; trying to squeeze augmented assignments onto one line does not improve readability or comprehension of what is happening.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • OK thanks. I wasn't really considering readability as it was just a personal little project. However I have a question, what is actually more efficient? Will a, b, c = 1, 2, 3 actually execute faster? – Theo Pearson-Bray Jan 17 '15 at 18:16
  • 1
    @TheoPearson-Bray: The difference is not worh caring about, but tuple assignment with 3 elements executes *two* more bytecodes than direct assignment would. See [How does swapping of members in the python tuples (a,b)=(b,a) work internally?](http://stackoverflow.com/a/21047622). Don't optimise for that tiny difference however. – Martijn Pieters Jan 17 '15 at 18:23