SyntaxError: cannot assign to function call here (Python)

avatar

Borislav Hadzhiev

Last updated: Apr 25, 2022

banner

Photo from Unsplash

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'

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

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 my_num = get_num() print(my_num) # 👉️ 100

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).

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') = 'Alice'

The error is caused because we used parenthesis 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'] = 'Alice' print(my_dict['name']) # 👉️ 'Alice'

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

main.py
def get_num(): return 100 if 100 == get_num(): # 👇️ this runs print('success') else: print('failure')

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

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.

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.