Python: How does multiple assignments in a single line work?

泄露秘密 提交于 2019-12-30 08:32:29

问题


I know that assignment is a statement in Python, i.e., it doesn't evaluate to a value unlike an expression. How does the following line of code work in Python, then? Please explain what happens internally in the Python interpreter (lexing, parsing, formation of abstract syntax tree).

# this works
spam = eggs = 'ham'

# this doesn't work. Throws SyntaxError
spam = (eggs = 'ham')

回答1:


why the first line above works while the second doesn't?

It's not about operator precedence. It's a designated syntax. It cannot be "reconcilliated" by adding parenthesis.

Now for the full answer (as @Rob's comments already indicate) see here and here.



来源:https://stackoverflow.com/questions/20088323/python-how-does-multiple-assignments-in-a-single-line-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!