There are times when you try to call the function the syntaxerror can’t assign to function call here raised in your program.
It means you’ve done something wrong in your code.
So today, we are going to deal with syntaxerror: cannot assign to function here error message.
If you are stuck to this error, then this article is indeed for you.
This article discusses important information that you need to know in order to fix this error.
What is “syntaxerror: cannot assign to function call here” error message?
The syntaxerror can’t assign to function call here occurs when you are trying to assign a value to a function call that cannot be granted by the Python interpreter.
This error message indicates that you are trying to assign a value to a function rather than trying to assign a function call to a variable.
In a nutshell, syntaxerror: cannot assign to function call here raised when you placed a function call on the left-hand side of the equal sign of a variable.
For example:
def add(x, y):
return x + y
add(10, 20) = 30
Output:
File "C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py", line 4
add(10, 20) = 30
^^^^^^^^^^^
SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
If you are confused and asking if syntaxerror: cannot assign to function call here and syntaxerror can’t assign to function call are the same.
Yes, they are the same, so you don’t have to worry. The solutions are the same as well.
Why does the “can’t assign to function call here” SyntaxError occurs in Python?
The can’t assign to function call here a SyntaxError occurs in Python when you are trying to attempt assigning a value to a function call.
This error arises because, in most programming languages, including Python, function calls are meant to invoke or execute the function, not to be assigned values directly.
In Python, when you write:
mysample_function() = 30
It violates the rules of language’s syntax because you’re trying to assign the value 30 to the function called mysample_function().
Python interprets this as a SyntaxError because it expects an identifier or a variable on the left side of the assignment operator (=) and not a function call.
How to fix “syntaxerror can’t assign to function call here”?
To fix the syntaxerror: cannot assign to function call here error in Python, ensure that you are not trying to assign a value to a function call. Instead, you should assign the value returned by the function call to a variable.
Here are the following solutions that you can use as your basis to resolve the error you are currently facing.
Solution 1: Specify the function call
You have to specify first the variable name on the left side, followed by an equals sign, and then specify the function call on the right side.
Incorrect code
def add(x, y):
return x + y
add(10, 20) = 30
Rather than writing add(10, 20) = 30, you should write result = add(10, 20), where the result is a variable that can store the value returned by the function call add(10, 20).
Corrected code
✅ def add(x, y):
return x + y
result = add(10, 20)
print(result)
In the corrected code, we assign the value returned by the function call add(10, 20) to the variable result instead of trying to assign it directly to the function call.
Output:
30
Solution 2: Use double equals
If you want to perform equality comparison, you can use double equals.
For example:
✅ a = 50
b = 100
if a == b:
print("a and b are equal")
else:
print("a and b are not equal")
Output:
a and b are not equal
Another example:
✅ website = 'itsourcecode'
if website == 'itsourcecode':
print('success')
else:
print('failure')
Output:
success
Solution 3: Use square brackets
If you encounter this error while working with the dictionary, it is because we use parentheses rather than using brackets [] when adding a new key-value to the dict.
For example:
sample_dict = {}
sample_dict('website') = 'Itsourcecode.com'
Note: Ensure to use square brackets “[]” when you need to access a key-value pair in a dictionary.
Corrected code:
✅ sample_dict = {}
sample_dict['website'] = 'Itsourcecode.com'
print(sample_dict['website'])
Output:
Itsourcecode.com
Solution 4: Use the correct syntax
If you encounter this error when dealing with list comprehensions, it is because you are using it incorrectly. You should use the correct syntax to avoid this error.
For example:
def website():
return 'itsourcecode'
result = [char for website() in char]
print(result)
As you can see in this example code, the order in the list comprehension is not correct. So as a result, it will throw an error message.
Output:
File "C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py", line 5
result = [char for website() in char]
^^^^^^^^^
SyntaxError: cannot assign to function call
Rather than using result = [char for website() in char], it should result = [char for char in website()].
Corrected code:
✅ def website():
return 'Itsourcecode'
result = [char for char in website()]
print(result)
Output:
['I', 't', 's', 'o', 'u', 'r', 'c', 'e', 'c', 'o', 'd', 'e']
Additional solutions for “syntaxerror: cannot assign to function call here”
If the above solutions do not resolve the error, maybe because you miss something. You can check the following solutions that can help you fix the error.
Double-check your code
Carefully examine the line of code where the error occurs and identify the specific function call that caused the problem.
Check variable assignment
Verify that you are attempting to assign the return value of the function call to a variable. If not, you may need to rethink your code logic.
Separate the assignment and function call
Rather than assigning value to the function call directly, you can use a temporary variable to store the result of the function call first. And then assign that variable to the desired target.
Conclusion
In conclusion, the error message syntaxerror can’t assign to function call here occurs when you are trying to assign a value to a function call that cannot be granted by the Python interpreter.
This error message indicates that you are trying to assign a value to a function rather than trying to assign a function call to a variable.
To fix this SyntaxError error in Python, ensure that you are not trying to assign a value to a function call. Instead, you should assign the value returned by the function call to a variable.
This article already discussed what this error is all about and multiple ways to resolve this error.
By executing the solutions above, you can master this SyntaxError with the help of this guide.
You could also check out other SyntaxError articles that may help you in the future if you encounter them.
- Uncaught syntaxerror: invalid or unexpected token
- Syntaxerror invalid character in identifier
- Multiple statements found while compiling a single statement
We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊