Python Forum

Full Version: [split] [split] New to the forum, how to post a question?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Please find my code , I am getting below error

class it :
 def rec (self,c,id,ac,bd,ad,bc):
  return (c + id)(ac - bd) + (ad + bc)

a = it()
a.rec(45,56,67,89,89,45)
I am getting error as type error : "int object is not callable "
please help me
you need an operator between (c + id)(ac - bd)
thus:
class it :
 def rec (self,c,id,ac,bd,ad,bc):
  return (c + id)*(ac - bd) + (ad + bc)
 
a = it()
a.rec(45,56,67,89,89,45)
Python is having a difficult time parsing this:
(c + id)(ac - bd)
Parenthesis can be used to group operations, used to create tuples, used to call a function, used to call a method.

Python can tell "(c + id)" is not a tuple, because tuples need commas. It is also not a function or method call because there is nothing before the parenthesis. It must be an operation surrounded by grouping parenthesis. The result of the addition is 101. So now Python is looking at this:
101(ac - bd)
This looks like a function call, but 101 is an int, not a function, so Python raises an error.

It may seem silly that python thinks "(c+id)(ac - bd)" is a function call. How can the result of an operation "(c + id)" be a function? Actually, this kind of thing happens all the time in Python. In this example a dictionary lookup returns a function. Python cannot know this when it is parsing the code. It has to rely on "object()" is a function call, so lets see if "object" is callable. In this example the object is a function, so it is callable. In you example the object was an int which is not callable.
import operator

operators = {"+":operator.add, "-":operator.sub, "*":operator.mul, "/":operator.floordiv}

def solve(equation):
    a, op, b = equation.split(" ")
    return operators.get(op)(int(a), int(b))

print(solve("5 + 4"))
Output:
9