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

A hand holding the Python logo
Photo by RealToughCandy.com, source: Pexels

🚫 SyntaxError: cannot assign to function call

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.

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.

Author photo

Hey 👋 I'm a software engineer, an author, and an open-source contributor. I enjoy helping people (including myself) decode the complex side of technology. I share my findings on Twitter: @lavary_

If you read this far, you can tweet to the author to show them you care. Tweet a Thanks

In this article:

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