I am trying to print a unpacked iterable, or a string if said iterable is empty in Python 3.9 (also happens in 3.10), and I am getting some unexpected behavior. print
is applying the star operator to the whole Iterable or str
expression, regardless of parentheses:
>>> print(*[1, 2, 3] or 'my string', sep=' ')
1 2 3
>>> print(*[] or 'my string', sep=' ')
m y s t r i n g
>>>
I would have expected print(*[] or 'my string', sep=' ')
to print my string
.
The *
operator definitely supports parentheses, for example:
>>> print(*([1, 2, 3] or 'my string'), sep=' ')
1 2 3
>>> print(*([] or 'my string'), sep=' ')
m y s t r i n g
>>>
In which case, the result would make sense. Is this expected behavior? If so, is it documented somewhere? If not, does anyone know if it has been addressed/plans exist to fix it in the next version of python?