How to fix "SyntaxError: cannot assign to function call here" in Python
Tags: python, programming
Published: 2023-02-02 20:05:32

> ✋ **Update:** This post was originally published on my blog [decodingweb.dev](https://www.decodingweb.dev), where you can read the [latest version](https://www.decodingweb.dev/python-syntaxerror-cannot-assign-to-function-call-here-fix) 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:


```python
# Raises 🚫 SyntaxError
f() = 23
```

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

```python
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. <u>Using the assignment operator (`=`) instead of the equality operator (`==`)</u>
2. <u>Wrong left-hand side operand</u>
3. <u>Incomplete list comprehension</u>

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:

```python
def my_func():
    return 12

# 🚫 SyntaxError
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:

```python
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. 

```python
def get_age():
    return 25

# 🚫 SyntaxError
get_age() = age
```

If your code looks like the above, switch sides:

```python
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:

```python
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:

```python
# 🚫 SyntaxError
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.

❤️ **You might like:**

[SyntaxError: 'break' outside loop in Python (Solved)](https://www.decodingweb.dev/python-syntaxerror-break-outside-loop-fixed)
[SyntaxError: invalid decimal literal in Python (Solved)](https://www.decodingweb.dev/python-syntaxerror-invalid-decimal-literal-fix)
[TypeError: can only concatenate str (not "list") to str (Fixed)](https://www.decodingweb.dev/python-typeerror-can-only-concatenate-str-not-list-to-str-fix)
[TabError: inconsistent use of tabs and spaces in indentation (Python)](https://www.decodingweb.dev/python-taberror-inconsistent-use-of-tabs-and-spaces-in-indentation-fix)
[How to fix the "TypeError: unhashable type: 'dict'" error in Python](https://www.decodingweb.dev/python-typeerror-unhashable-type-dict-fixed)