Can’t Assign to Function Call in Python

When programming with Python, the understanding of the “SyntaxError: cannot assign to function call” error boils down to understanding these three topics:

  • What is SyntaxError?
  • Understanding variable assignment and
  • How to call a function.

Let’s discuss those three before we go into the actual causes of the “Can’t Assign to Function Call” error.

What is SyntaxError?

A SyntaxError is a Python error raised when our code violates the language’s predefined rules.

Understanding variable assignment

One of the Python rules governs how to define a variable. It states that (paraphrased):

When declaring a variable, the variable name comes first, followed by an assignment operator (=), then the value“.

Going against that rule leads to SyntaxError.

Function call

A Python function is called by using its name followed by parentheses. If the function accepts arguments, then pass the arguments inside the parenthesis, for example.

Note that a Python function returns a value (thus, a function call evaluates to a value). Therefore, we can assign the function call to a variable, as shown below.

Output:

33.599999999999994

We are now ready to discuss some common causes of “Can’t Assign to Function Call” and their solutions.

Causes and Solutions to “Can’t Assign to Function Call” Error

This Section discusses four common causes of the error and how to solve each.

Case 1: Specifying a function call to the left-hand side of the assignment operator (=)

As stated earlier, the variable assignment rule requires the variable name to be on the left side of the “=” operator and the value on the right.

That is also true for the function call (because it returns a value) – a function call should be defined on the right-hand side of the assignment operator. Doing the opposite leads to the “Can’t Assign to Function Call” error, for example.

Output:

Solution

The correct way to assign a function call is to keep it on the right side of the “=” operator and the variable name on the left. For example,

Output:

1215.0
254.8

Case 2: Confusing comparison and assignment operator

Sometimes, you use the assignment operator (=) when you meant to use the comparison operator (==).

If you want to compare the result of the function call with another value, use the comparison operator (==), as shown below.


print(Add2(3, 4) == 7)

Output:

True

Add2(3,4) returns 7, therefore, Add2(3, 4) == 7 evaluates to True.

Case 3: Using square brackets when assigning an item to a dictionary or list

You can also face the “Can’t Assign to Function Call” error when you attempt to assign or add an item into a dictionary using parentheses, as shown below.

Output:

SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?

You will also experience the same when updating an element in a list.

Output:

SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?

Solution

Use square brackets (not parentheses) to update an item in a list or Python dictionary, as shown in the following examples.

Output

[5, 'San Diego', 'Allan']
{'Id': 1, 'City': 'New York', 'Name': 'Allan'}

Case 4: Incorrect syntax when using the “in” operator in a loop

Here is an example of that.

Output:

SyntaxError: cannot assign to function call

Note that the order in the list comprehension is incorrect. We should iterate over the result of a function call, not over the element of the return value of the function. The correct code should be

Output:

['S', 'm', 'i', 't', 'h']

Conclusion

This post discussed the causes and solutions to the “SyntaxError: cannot assign to function call” error. After going through the four common cases that lead to the error discussed in this article, you should be able to identify and fix this error when it arises.