在缩短我的代码时,我将一些变量声明减少到一行 -
##For example- going from-
Var1 =15
Var2 = 26
Var3 = 922
##To-
Var1, Var2, Var3 = 15, 26, 922
但是,当我尝试对此代码执行相同操作时 -
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]
这会引发错误
SyntaxError: illegal expression for augmented assignment
我已阅读relevant Python documentation,但我仍无法找到缩短此特定代码的方法。
答案 0 :(得分:9)
您可以在链接到的Augmented assignment statements section中看到这一点:
augmented_assignment_stmt ::= augtarget augop (expression_list | yield_expression) augtarget ::= identifier | attributeref | subscription | slicing
augtarget
规则仅允许一个目标。将其与Assignment statements rules:
assignment_stmt ::= (target_list "=")+ (expression_list | yield_expression) target_list ::= target ("," target)* [","] target ::= identifier | "(" target_list ")" | "[" target_list "]" | attributeref | subscription | slicing
您要分配target_list
规则。
我根本不试图缩短它;试图将增强的作业压缩到一行上不提高可读性或理解正在发生的事情。