Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use += on multiple variables on one line?

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.

like image 219
Theo Pearson-Bray Avatar asked Jan 17 '15 18:01

Theo Pearson-Bray


People also ask

Can I declare multiple variables in single line?

Declaring multiple variables in a single declaration can cause confusion regarding the types of the variables and their initial values. If more than one variable is declared in a declaration, care must be taken that the type and initialized value of the variable are handled correctly.

Can you use more than 1 variable in a program?

You can assign multiple values to multiple variables by separating variables and values with commas , . You can assign to more than three variables. It is also possible to assign to different types. If there is one variable on the left side, it is assigned as a tuple.

Can you define multiple variables in one line C?

If your variables are the same type, you can define multiple variables in one declaration statement. For example: int age, reach; In this example, two variables called age and reach would be defined as integers.

Can you declare multiple variables on one line Java?

As we've known, we can declare and assign multiple variables in one single line in Java. In this tutorial, we'll explore how to do the same in Kotlin.


1 Answers

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.

like image 130
Martijn Pieters Avatar answered Oct 22 '22 11:10

Martijn Pieters