SyntaxError: cannot assign to function call here in Python (Solved)

Updated Sep 22, 2023 β€³ 4 min read

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: cannot assign to function call here. Maybe you meant '==' instead of '='?
f() = 23

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 '='?

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.

🎧 Debugging Jam

Calling all coders in need of a rhythm boost! Tune in to our 24/7 Lofi Coding Radio on YouTube, and let's code to the beat – subscribe for the ultimate coding groove!" Let the bug-hunting begin! πŸŽ΅πŸ’»πŸš€

24/7 lofi music radio banner, showing a young man working at his computer on a rainy autmn night with hot drink on the desk.

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

# Raises 🚫 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
if my_func() = 12:
    print ('Passed')

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

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

# Raises 🚫 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
get_age() = age

If your code looks like the above, switch sides:


def get_age():
    return 25

age = get_age()

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]

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:


# Raises 🚫 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
squares = [x**2 in x for range(1, 10)]

print(squares)

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.

Disclaimer: This post may contain affiliate links. I might receive a commission if a purchase is made. However, it doesn’t change the cost you’ll pay.

`