SyntaxError: cannot assign to function call here in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# SyntaxError: cannot assign to function call here (Python)

The Python "SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?" occurs when we try to assign to a function call.

To solve the error, specify the variable name on the left, and the function call on the right-hand side of the assignment.

syntaxerror cannot assign to function call here

Here is an example of how the error occurs.

main.py
def get_num(): return 100 # ⛔️ SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='? get_num() = 'abc'

cannot assign to function call here

The error is caused because we are trying to assign a value to a function call.

# Specify the function call on the right-hand side

To solve the error, make sure to specify the function call on the right-hand side of the assignment.

main.py
def get_num(): return 100 # ✅ Call the function on the right-hand side of the assignment my_num = get_num() print(my_num) # 👉️ 100

call function on right hand side

Make sure the function returns what you expect because functions that don't explicitly return a value return None.

When declaring a variable, specify the variable name on the left-hand side and the value on the right-hand side of the assignment (=).

You can think of a variable as a container that stores a specific value (e.g. the value that the function returns).

The parentheses are used to invoke the function.

If your function takes parameters, make sure to specify them.

main.py
def greet(first, last): return 'hello ' + first + ' ' + last result = greet('bobby', 'hadz') print(result) # 👉️ hello bobby hadz

# Use double equals when performing an equality comparison

If you meant to perform an equality comparison, use double equals.

main.py
def get_num(): return 100 # ✅ Using double equals when comparing values if 100 == get_num(): # 👇️ This runs print('success') else: print('failure')

We use double equals == for comparison and single equals = for assignment.

main.py
# ✅ Assignment (single equals) name = 'bobby' # ✅ Equality comparison (double equals) if name == 'bobby': print('success')

# Use square brackets when adding keys to a Dictionary

You might also get the error when working with a dictionary.

main.py
my_dict = {} # ⛔️ SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='? my_dict('name') = 'Bobby'

The error is caused because we used parentheses instead of square brackets to add a new key-value pair to the dict.

Make sure to use square brackets if you have to access a key-value pair in a dictionary.

main.py
my_dict = {} my_dict['name'] = 'Bobby' print(my_dict['name']) # 👉️ 'Bobby'

Square brackets are used to add key-value pairs to a dictionary and to access keys in a dictionary.

# Use square brackets when updating or accessing elements in a list

You should also use square brackets when updating or accessing elements in a list.

Here is an example of how the error occurs when parentheses are used.

main.py
my_list = ['bobby', 'hadz', 'com'] # ⛔️ SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='? my_list(0) = 'abc'

To resolve the issue, use square brackets [] instead.

main.py
my_list = ['bobby', 'hadz', 'com'] my_list[0] = 'abc' print(my_list) # 👉️ ['abc', 'hadz', 'com'] print(my_list[0]) # 👉️ abc

We used square brackets to update the value of the list element at index 0 and then accessed the element using square brackets.

# Valid names for variables and functions in Python

The name of a variable must start with a letter or an underscore.

A variable name can contain alpha-numeric characters (a-z, A-Z, 0-9) and underscores _.

main.py
def get_num(): return 100 my_num = get_num() print(my_num) # 👉️ 100

Note that variable names cannot start with numbers or be wrapped in quotes.

Variable names in Python are case-sensitive.

main.py
def get_num(): return 100 def get_num_2(): return 200 my_num = get_num() print(my_num) # 👉️ 100 MY_NUM = get_num_2() print(MY_NUM) # 👉️ 200

The 2 variables in the example are completely different and are stored in different locations in memory.

# Incorrect syntax when looping

You might also get the error when you have syntax errors in list comprehensions or generator expressions.

main.py
def get_name(): return 'bobby' result = [char for get_name() in char] # ⛔️ SyntaxError: cannot assign to function call print(result)

Notice that the order in the list comprehension is incorrect.

Instead, it should be [char for char in get_name()].

main.py
def get_name(): return 'bobby' result = [char for char in get_name()] # ['b', 'o', 'b', 'b', 'y'] print(result)

Make sure the order of operations in your for loops, generator expressions and list comprehensions is correct.

We iterate over the result of calling the function, not over a character or element of the return value of the function.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.