Last updated: Apr 8, 2024
Reading time·3 min
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.
Here is an example of how the error occurs.
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.
def get_num(): return 100 # ✅ Call the function on the right-hand side of the assignment 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
.
=
).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.
def greet(first, last): return 'hello ' + first + ' ' + last result = greet('bobby', 'hadz') print(result) # 👉️ hello bobby hadz
If you meant to perform an equality comparison, use double equals.
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.
# ✅ Assignment (single equals) name = 'bobby' # ✅ Equality comparison (double equals) if name == 'bobby': print('success')
You might also get the error when working with a dictionary.
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.
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.
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.
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.
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.
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 _
.
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.
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.
You might also get the error when you have syntax errors in list comprehensions or generator expressions.
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()]
.
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.
You can learn more about the related topics by checking out the following tutorials: