Fix the SyntaxError: Can't Assign to Function Call in Python

Fix the SyntaxError: Can't Assign to Function Call in Python

  1. Syntax Error in Python
  2. Fix the SyntaxError: can't assign to function call in Python

This article will discuss how to fix the SyntaxError: can't assign to function call error in Python.

Syntax Error in Python

The syntax of computer programming is the grammar or proper writing structure that developers must follow efficiently to avoid bugs in code. Like there are naming conventions and defined structures of loops and conditions you need to follow; otherwise, your code will not be executed.

It is mandatory to follow the rules and regulations of programming language to code properly and avoid bugs. Let us understand it through an example.

for x in range(1,6): # this will print 1,2,3,4,5
    print(x, end= " ")

Output:

1 2 3 4 5

The above program demonstrates a proper definition of a for loop in Python. If you write a for loop in an undefined manner in Python, it will throw a Syntax Error.

for in x range(1,6): # this will print 1,2,3,4,5
    print(x, end= " ")

Output:

SyntaxError: invalid syntax

See, we have just changed the positions of in and x in the above for loop. The Python compiler did not support this syntax, throwing a Syntax Error.

This is why following the defined syntax is mandatory. Otherwise, the Python compiler will throw a Syntax Error.

Fix the SyntaxError: can't assign to function call in Python

In Python, the can't assign to function call error occurs when you try to assign a variable or a value to the function, which is not allowed or against the syntax of Python. You can assign a function to a variable but not a variable to a function.

Let us understand it through an example.

"Delft Stack" = name

Output:

SyntaxError: can't assign to literal

The above statement is invalid in Python. You cannot assign a variable to a string, but the opposite is possible.

name = "Delft Stack"
print(name)

Output:

Delft Stack

This is the right syntax acceptable in Python, so it is executed without causing any error. Similarly, you cannot assign a variable to a function, but the opposite is possible.

Let’s understand it through an example.

class Greetings():
    def hi(self):
        return "Hey! How are you?"

Delft = Greetings()
Delft.ftn() = x #This statement is invalid
print(x)

Output:

SyntaxError: can't assign to function call

In the above program, the statement Delft.ftn() = x is not supported by the Python compiler because the syntax is incorrect; that’s why it has thrown a Syntax Error. Let’s change the assignment order of this Delft.ftn() = x statement to fix the Syntax Error.

Let’s fix the error can't assign to function call in Python.

class Greetings():
    def hi(self):
        return "Hey! How are you?"

Delft = Greetings()
x = Delft.hi()
print(x)

Output:

Hey! How are you?

As you can see, this program is now executed without causing any error. This statement x = Delft.hi() is now in the proper order; it follows Python’s defined syntax.

Zeeshan Afridi avatar Zeeshan Afridi avatar

Zeeshan is a detail oriented software engineer that helps companies and individuals make their lives and easier with software solutions.

LinkedIn

Related Article - Python Error

  • Python PermissionError: [WinError 5] Access Is Denied
  • Python TypeError: 'DataFrame' Object Is Not Callable
  • Python TypeError: Can't Convert 'List' Object to STR
  • Local Variable Referenced Before Assignment Error in Python
  • Python Handling Socket.Error: [Errno 104] Connection Reset by Peer
  • Python Is Not Recognized in Windows 10