[issue28550] if inline statement does not work with multiple assignment.

Eric Snow report at bugs.python.org
Fri Oct 28 14:25:45 EDT 2016


Eric Snow added the comment:

The syntax is working correctly.  Precedence rules are throwing you off.  It should be more clear if we use parentheses to demonstrate precedence explicitly.

You expected:

a, b = (obj.a, obj.b) if obj else [None, None]

However, what you wrote is equivalent to:

a, b = obj.a, (obj.b if obj else [None, None])

Hence the error about not finding "None.a" (when it resolves obj.a).  You can solve this by explicitly marking the precedence like I did in the "You expected" example above.  Sometimes precedence isn't clear, so when in doubt, use parentheses to make the precedence explicit.

I'm sorry this wasn't more clear.  Do you think any changes in Python's documentation would have helped you avoid this confusion?

----------
nosy: +eric.snow
resolution:  -> not a bug
stage:  -> resolved
status: open -> pending
type: crash -> behavior

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28550>
_______________________________________


More information about the Python-bugs-list mailing list