HOWTO · Python
How to Fix the SyntaxError: Can't Assign to Function Call in Python
Assigning a function name to a variable stores a reference to the function object. To store its return value, call it (for example, `greet()`) on the right side of an assignment, such as `result = greet()`.
On this page
This error means Python found a function call where an assignment target must go. In an assignment such as target = value, Python evaluates the right side and stores that value in a writable location on the left. A call like make_item() produces a value; it is not itself a location where another value can be stored.
What the error means
SyntaxError: can't assign to function call points to an assignment whose left side contains a call, commonly because the sides were written in the wrong order. Python rejects the statement while parsing the file, before it runs any of the program’s statements. The diagnostic can therefore appear even if the function has not been defined or called yet.
Look at the assignment operator = and read the expression immediately to its left. If that expression ends in parentheses that call a function or method, ask what you meant to change: the returned value, the function object, or a part of an object. Those intentions use different assignment forms.
Why a function call cannot be an assignment target
An assignment target must identify a place that can hold a value, such as a variable, an object’s attribute, or an item in a mutable container. Parentheses after a function name invoke it. The call evaluates to its return value, which may be a string, number, object, or None; the returned value is not a writable slot in the caller’s code.
For example, build_user() = user is invalid: Python cannot put user into the result of build_user(). This is a syntax problem, not a complaint about the function’s return type. Changing the function to return a different type will not make the call itself a valid target.
A function name without call parentheses is different. It refers to the function object, so a variable can be made to refer to that same callable. Likewise, a call can appear on the right side of an assignment, where its result is evaluated and stored in a valid target.
Fix the assignment based on your intent
If you want to keep the value returned by a call, put a variable on the left and the call on the right. This self-contained example uses the method from the original article and gives the result a clear name.
Run this example:
class Greetings:
def hi(self):
return "Hey! How are you?"
Delft = Greetings()
message = Delft.hi()
print(message)
The variable message receives the string returned by hi. The assignment target is the variable; Python calls Delft.hi() to produce the value on the right. Use the name that fits the value’s role in your program, and use that variable later instead of trying to assign back into the call.
If you meant to store the function itself for later use, omit the call parentheses: saved_function = greet. Calling saved_function() later runs it.
There is an important boundary: a call cannot itself be the target, but an attribute or item of a writable object returned by a call can be. This example changes an attribute on the object returned by get_user():
class User:
pass
user = User()
def get_user():
return user
get_user().name = "Ada"
Here, get_user() produces the object, and .name identifies the writable attribute. The assignment works only when the returned object supports that attribute update. The same distinction applies to an item such as get_items()[0] = "first": the call supplies the container, while [0] selects the target. A returned immutable value may not support the requested update.
A practical correction process is to locate the = in the failing statement, identify the expression on its left, then state your intent in plain words. “Save what this function returns” means assign the call’s result to a variable. “Save this function for later” means assign the function name without calling it. “Change one field or item” means target an attribute or subscript that belongs to a writable object. Do not simply swap text around without checking which value should be stored where.
Check related assignment errors
This error is distinct from SyntaxError: can't assign to literal, which points to a literal value such as a string being used as the assignment target. In "Delft Stack" = name, the string itself cannot be changed into a variable. Write name = "Delft Stack" when you want the variable to refer to that string.
When a syntax diagnostic mentions an assignment, inspect the left side first. A function call, literal, or other expression that does not identify a writable location cannot receive a value. Keep the desired value on the right and choose a valid target on the left. If the target is a variable, attribute, or subscript and the code still fails, check the exact diagnostic: later errors may concern whether an object supports that particular update, which is a different issue from assigning directly to a call.