New submission from JD-Veiga <[email protected]>:
I was trying to return the concatenation of several lists from a function but,
inadvertently, forgot to surround the multi-line concatenation expression with
parentheses.
As a result, the function returns just the first operand and does not perform
the concatenation at all.
Basically, I am doing something like this:
```
def non_parens():
return [
1, 2, 3
]
+ [
4, 5
]
print(non_parens())
```
which outputs: `[1, 2, 3]`
On the contrary, parenthesised version such as:
```
def with_parens():
return (
[
1, 2, 3
]
+ [
4, 5
]
)
print(with_parens())
```
will output the "expected" result: `[1, 2, 3, 4, 5]`.
Even not breaking the line before the '+' operator:
```
def no_parens_without_newline_before_operator():
return [
1, 2, 3
] + [
4, 5
]
print(no_parens_without_newline_before_operator())
```
prints `[1, 2, 3, 4, 5]`.
My hypothesis is that first function `non_parens()` does not work because the
expression has some kind of error after
```
return [
1, 2, 3
]
```
and
```
+ [
4, 5
]
```
is never executed.
However, why does not the parser or the interpreter raise any error or warning
about this malformed expression?
"Similar" expressions cause an error:
```
assert [
1, 2, 3
]
+ [
4, 5
]
```
raises: `IndentationError: unexpected indent`
and
```
[
1, 2, 3
]
+ [
4, 5
]
```
raises: `TypeError: bad operand type for unary +: 'list'`
What perplexes me the most about breaking lines in this case is that `] + [`
works and `]<newline>+ [` does not (against PEP8 --of course, PEP8 is not the
parser).
I am sure that I am missing something about expressions, line breaks, or
lexical parsing.
I am using Python 3.8.7 and Python 3.9.1
Thank you a lot.
----------
components: Interpreter Core
messages: 384854
nosy: JD-Veiga
priority: normal
severity: normal
status: open
title: Return multi-line list concatenation without parentheses returns only
first operand
type: behavior
versions: Python 3.8, Python 3.9
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue42895>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com