This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Unpacking tuple argument in combination with inline if statement
Type: behavior Stage: resolved
Components: Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: steven.daprano, wietse.j
Priority: normal Keywords:

Created on 2021-01-25 08:42 by wietse.j, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg385607 - (view) Author: Wietse Jacobs (wietse.j) Date: 2021-01-25 08:42
I suspect that I found a bug. If I run the following script:

```
def f(t=None):
    t0, t1 = t if t is not None else [], []
    return t0, t1


def g(t=None):
    if t is None:
        t = [], []
    t0, t1 = t
    return t0, t1


def test():
    res_f = f(t=([1, 1], [2, 2]))
    res_g = g(t=([1, 1], [2, 2]))
    assert res_f == res_g, f'{res_f=} != {res_g=}'


test()
```

I get an assertion error, with:

```
res_f=(([1, 1], [2, 2]), []) != res_g=([1, 1], [2, 2])
```

I expected them to be equal.
msg385608 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-01-25 09:31
This is a operation precedence issue.

The line:

    t0, t1 = t if t is not None else [], []

is parsed as:

    (t if t is not None else []), []


so you need brackets around the "else" operand to get the effect you want.

    t0, t1 = t if t is not None else ([], [])
History
Date User Action Args
2022-04-11 14:59:40adminsetgithub: 87187
2021-01-25 09:31:35steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg385608

resolution: not a bug
stage: resolved
2021-01-25 08:42:55wietse.jcreate