classification
Title: if inline statement does not work with multiple assignment.
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.snow, levinvm
Priority: normal Keywords:

Created on 2016-10-28 18:07 by levinvm, last changed 2016-10-31 17:51 by eric.snow. This issue is now closed.

Messages (2)
msg279622 - (view) Author: Levi Velázquez (levinvm) Date: 2016-10-28 18:07
Normal "if" inline assignment

obj = None
a = obj.a if obj else None

It assign None to a as expected. 

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

It raises an error  " 'NoneType' object has no attribute 'a'"
when it should assign None to both a and b.
msg279623 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2016-10-28 18:25
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?
History
Date User Action Args
2016-10-31 17:51:13eric.snowsetstatus: pending -> closed
2016-10-28 18:25:45eric.snowsetstatus: open -> pending

type: crash -> behavior

nosy: + eric.snow
messages: + msg279623
resolution: not a bug
stage: resolved
2016-10-28 18:07:05levinvmcreate