DEV Community

Cover image for How to fix "SyntaxError: cannot assign to function call here" in Python
Reza Lavarian
Reza Lavarian

Posted on • Originally published at decodingweb.dev

How to fix "SyntaxError: cannot assign to function call here" in Python

Update: This post was originally published on my blog decodingweb.dev, where you can read the latest version for a 💯 user experience. ~reza

Python raises “SyntaxError: cannot assign to function call here. Maybe you meant ‘==’ instead of ‘=’?” when a function call is the left-hand side operand in a value assignment:

# Raises 🚫 SyntaxError
f() = 23
Enter fullscreen mode Exit fullscreen mode

Python also provides you a hint, assuming you meant to use the equality operator (==):

File /dwd/sandbox/test.py, line 4
  my_func() = 12
  ^^^^^^^^
SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Enter fullscreen mode Exit fullscreen mode

Most of the time, the reason is a typo in your code - usually a missing =.

How to fix "SyntaxError: cannot assign to function call"

The long error "SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?" occurs under various scenarios:

  1. Using the assignment operator (=) instead of the equality operator (==)
  2. Wrong left-hand side operand
  3. Incomplete list comprehension

Let's see some examples.

Using the assignment operator (=) instead of the equality operator (==): One of the most common causes of this SyntaxError is using the assignment operator (=) instead of the equality operator (==) in value comparisons:

def my_func():
    return 12

# 🚫 SyntaxError
if my_func() = 12:
    print ('Passed')
Enter fullscreen mode Exit fullscreen mode

In the above example, we're trying to compare the return value of my_func() to the value of 12. However, the = operator isn't a comparison operator.

To fix the error, we use the equality operator (==) instead:

def my_func():
    return 12

if my_func() == 12:
    print ('Passed')

# Output: Passed
Enter fullscreen mode Exit fullscreen mode

Wrong left-hand side operand (in assignment statements): Assignment statements bind names to values. (e.g., age = 25)

Based on Python syntax and semantics, the left-hand side of the assignment operator (=) should always be an identifier - an arbitrary name you choose for a specific value (a.k.a variable). For instance, age for 25.

That said, if you have a function call as the left-hand side operand, you'll get the "SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?" error.

def get_age():
    return 25

# 🚫 SyntaxError
get_age() = age
Enter fullscreen mode Exit fullscreen mode

If your code looks like the above, switch sides:

def get_age():
    return 25

age = get_age()
Enter fullscreen mode Exit fullscreen mode

Incomplete list comprehension: A confusing scenario that leads to this SyntaxError is a wrong list comprehension statement.

We usually use list comprehensions to generate a list where each element is the result of an operation applied to each member of another sequence or iterable.

Imagine you have a range object from 1 to 10, and you want to calculate the square root of each element and store them in a separate list.

A list comprehension statement consists of brackets containing an expression (e.g., x**2) followed by a for clause (e.g., for x in range(1, 10)).

We can implement the above example like so:

squares = [x**2 for x in range(1, 10)]

print(squares)
# Output: [1, 4, 9, 16, 25, 36, 49, 64, 81]
Enter fullscreen mode Exit fullscreen mode

In the above example, x**2 is applied to every item in our range. Now, if you accidentally replace the for keyword with in, you'll get this SyntaxError:

# 🚫 SyntaxError
squares = [x**2 in x for range(1, 10)]

print(squares)
Enter fullscreen mode Exit fullscreen mode

The reason is whatever follows for is supposed to be the variable that holds the current item on each iteration.

Alright, I think it does it. I hope this quick guide helped you solve your problem.

Thanks for reading.

❤️ You might like:

SyntaxError: 'break' outside loop in Python (Solved)
SyntaxError: invalid decimal literal in Python (Solved)
TypeError: can only concatenate str (not "list") to str (Fixed)
TabError: inconsistent use of tabs and spaces in indentation (Python)
How to fix the "TypeError: unhashable type: 'dict'" error in Python

Top comments (0)