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
  Class test : good way to split methods into several files paul18fr 4 490 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  [split] Pipenv mohammadasadi4 0 309 Jan-15-2024, 10:35 AM
Last Post: mohammadasadi4
  [split] Why is there an output of None akbarza 1 469 Nov-27-2023, 02:53 PM
Last Post: deanhystad
  [split] Class takes no arguments bily071 2 650 Oct-23-2023, 03:59 PM
Last Post: deanhystad
  [split] Issue installing selenium Akshat_Vashisht 1 551 Oct-18-2023, 02:08 PM
Last Post: Larz60+
  doing string split with 2 or more split characters Skaperen 22 2,563 Aug-13-2023, 01:57 AM
Last Post: Skaperen
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,161 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  How to "tee" (=split) output to screen and into file? pstein 6 1,409 Jun-24-2023, 08:00 AM
Last Post: Gribouillis
  [split] How to resolve version conflicts in Python? atonalwilson 1 1,006 May-04-2023, 09:02 AM
Last Post: buran
  [split] Parse Nested JSON String in Python mmm07 4 1,549 Mar-28-2023, 06:07 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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