Issue
In C++, you sometimes have a situation where assigning to a function call makes sense--my understanding is that this is permissible when the function call returns an lvalue. So you might have:
some_function() = some_value;
In Python, it's not quite the same. Based on my understanding of the language, I would assume that there's never a time to assign to a function call. Yet if you try, you get a somewhat cryptic error message that suggests it may be possible. Here's a fairly minimal example (run with Python 3.10.8 and IPython 8.6.0):
In [1]: enumerate() = x
Cell In [1], line 1
enumerate() = x
^
SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Because it says, "cannot assign to function call here", it seems implied there are cases where you might.
I'm wondering why that error message is as it is. Should it really say, "cannot assign to function call (at all)"? I expect this is the case, but I'm curious if there's some esoteric/special case that the error message is referencing when it says, "cannot assign to function call here".
I've tried searching this but can only find questions for people trying to resolve this error, for instance this question. To be clear, I am not trying to resolve this error; I understand what's wrong (and there are quite a few things wrong) with enumerate() = x
. I'm merely curious why IPython makes it sound like there may be some situation where it is appropriate syntax to say some_function() = some_value
.
Is there something, or is it as I expect that this is just a confusingly written error message? Perhaps it just means "here" is the line/column where the error is? You'd think the arrow/carrot ^ pointing to it would be sufficient were that the case.
Solution
You can't assign to a function call. It's just a confusing error message.
The error message comes from a general rule for generating syntax errors for invalid assignments:
invalid_named_expression(memo):
| a=expression ':=' expression {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
| a=NAME '=' b=bitwise_or !('='|':=') {
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?") }
| !(list|tuple|genexp|'True'|'None'|'False') a=bitwise_or b='=' bitwise_or !('='|':=') {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot assign to %s here. Maybe you meant '==' instead of '='?",
_PyPegen_get_expr_name(a)) }
which covers more cases than just function calls. For example, if you write 1, a.b = 2
, you get
>>> 1, a.b = 2
File "<stdin>", line 1
1, a.b = 2
^^^
SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
saying cannot assign to attribute here
, even though you can usually assign to attributes. That might have motivated the "here". Even in a case like this, the error message is still confusing, though - it shouldn't say anything about the attribute. It should point to the real problem, which is that this code is trying to assign to 1
.
Answered By - user2357112 Answer Checked By - Terry (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.