Python

How to Solve Python SyntaxError Can’t Assign to Function Call

In Python, a function call is made by accessing a function by its name followed by some parentheses. When a function is accessed/called, Python executes the code within the function and returns the result. But sometimes while calling the function, the “SyntaxError: Can’t Assign to Function Call” error can occur in the program.

In this Python tutorial, we will review the main causes of the “SyntaxError: Can’t Assign to Function Call” error and provide solutions to fix it.

Causes of the “SyntaxError: Can’t Assign to Function Call” Error

The “Can’t Assign to Function Call” error appears when you attempt to provide a value to a function call. This can be caused by a variety of factors. Let’s discuss the following possible causes of this error and the solutions to cope with these causes one by one:

Cause 1: Incorrect Use of Assignment Operator

This syntax error occurs when we assign the function in an incorrect format while invoking it. For example, Python throws an error if a variable and a value or function call are placed on opposite sides of the assignment operator.

Example

Following is an example code:

def add(a,b):
  return a+b
add(2,3) = c

In the above code, the discussed “syntax error” appears because the syntax is incorrect. The statement “add(2,3) = c” is trying to assign the result of the function “add(2,3)” to the variable “c”, but this is not a valid assignment in Python.

Output

As seen, the SyntaxError: cannot assign to function call here” has been returned in the above output.

Solution

To fix the error, remove the “= c” part of the statement and just call the “add()” function with the arguments “2” and “3”, or use the correct syntax to assign the result of the function to variable “c”.

Example

This example explains the stated concept:

def add(a, b):
  return a+b
c = add(2, 3)
print(c)

In the above code, the function “add()” takes the two numbers as its arguments and adds them. Also, the function is allocated at the right of the variable appropriately.

Output

The above output shows the returned value of the “add()” function, thereby eliminating the faced exception.

Cause 2: Using Parentheses Instead of Brackets

The error can also occur when you call or access a function that returns a list using parentheses instead of brackets.

Example

Let’s overview the following example code:

def get_list():
  return [1, 2, 3]
get_list()(0) = 4

In the above code snippet, the discussed error occurs because we used parentheses instead of brackets while calling the function for modification of the returned list.

Output

As analyzed, the discussed limitation has been returned in the above output.

Solution

To resolve the error in this case, assign the result of the function to a variable, and then modify the first element of the list using indexing.

Example

Here’s how it works:

def get_list():
  return [1, 2, 3]
my_list = get_list()
my_list[0] = 4
print(my_list)

In the above code, “indexing” is carried out with the help of the square brackets to assign the stated value to a specific index via an additional variable.

Output

In this output, the specific value has been updated to the list returned by the user-defined function, thereby omitting the faced error.

Cause 3: Trying to Assign/Set a Value to a Function Call

The discussed error also occurs when we try to give a value while calling/accessing a function.

Example

For further understanding, let’s have a look at the below example:

def add(a,b):
  return a+b
c = add(2,3) = 5

In the above code block, the “c = add(2,3) = 5” statement is used to assign a value while invoking the function, thereby resulting in the discussed limitation.

Output

Solution

To cope with the error in this scenario, remove the latter “equal(=)” sign in the statement “c = add(2,3) = 5“. The correct way is to call the “add()” function and assign its result to the “c” variable.

Example

Go through the below-provided code:

def add(a, b):
  return a+b
c = add(2, 3)
print(c)

According to the above code, the “add()” function adds the two numbers by taking them as its arguments, and the function is simply assigned to the variable, thereby eliminating the assigned value.

Output

The above output shows the returned value of the “add()” function appropriately.

Cause 4: Using an Assignment Operator in an Expression

This error can also be encountered when we try to assign a value to the result of an expression instead of a variable.

Example

Here is an example code:

a = 2
b = 3
a + b = c

In the above lines of code, the statement line “a + b = c” assigns a value to an expression which is not possible.

Output

As observed, the stated limit is encountered.

Solution

In order to eliminate the error, assign the result of the expression to a variable rather than a string.

Example

Following is the corrected code:

a = 2
b = 3
c = a + b
print(c)

In this code block, create a new variable “c“, and use the assignment operator “=” to assign the result of the expression “a + b” to “c“.

Output


The two integer values have been added using the “+” operator and via correct allocation.

Conclusion

The “SyntaxError: Can’t Assign to Function Call” can occur due to various reasons like incorrect use of the “=” operator, using an assignment operator in an expression, or using parentheses instead of brackets, etc. This post elaborated on various causes and their solutions to cope with the discussed error using numerous examples.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.