Python Forum
[split] [split] New to the forum, how to post a question?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] [split] New to the forum, how to post a question?
#1
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
buran write Feb-12-2022, 11:07 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
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)
BashBedlam and karnik like this post
Reply
#3
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
karnik and ndc85430 like this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] ibm_db install problem SQLPython 1 538 Feb-13-2025, 07:24 PM
Last Post: buran
  [split] Newbie needs help Schoe1 0 381 Feb-12-2025, 06:57 PM
Last Post: Schoe1
Star how to split pdf under 10mb using python skchui9786 4 955 Jan-18-2025, 03:25 AM
Last Post: skchui9786
  [split] another problem with code blakeusherremix68 0 378 Dec-23-2024, 11:36 PM
Last Post: blakeusherremix68
  [split] Code help emma1423 1 568 Dec-13-2024, 02:00 PM
Last Post: perfringo
  [split] Prime numbers saima 1 520 Dec-09-2024, 02:19 AM
Last Post: jefsummers
Question [split] How to ask Smart Questions (thread title expansion) darkuser 4 1,413 Nov-11-2024, 01:27 PM
Last Post: deanhystad
  [split] Help with my coding happy_nutella 1 657 Oct-08-2024, 06:52 PM
Last Post: jefsummers
  Unable to understand the function string.split() Hudjefa 8 2,264 Sep-16-2024, 04:25 AM
Last Post: Pedroski55
  Class test : good way to split methods into several files paul18fr 5 3,498 Jul-17-2024, 11:12 AM
Last Post: felixandrea

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020