Python Forum
How to pass a method as argument in an another method?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to pass a method as argument in an another method?
#4
Hey thanks for the response. I guess we don't need an init function here. Checkout the trimmed the code.
#calculator class with arithmetic methods

class calc:
 
    def execute(self, func, a, b):
        self.a = a
        self.b = b
        if func == "add":
            self.add()
        elif func == "sub":
            self.sub()
        elif func == "mul":
            self.mul()
        elif func == "div":
            self.div()
 
    def add(self):
        print (self.a,"+",self.b,"=",self.a + self.b)
      
 
    def sub(self):
          print (self.a,"-",self.b,"=",self.a - self.b)
     
 
    def mul(self):
          print (self.a,"*",self.b,"=",self.a* self.b)
        
 
    def div(self):
        print (self.a,"/",self.b,"=",self.a / self.b)
       
 
 
cal = calc()
cal.execute("div", 6, 3)
Reply


Messages In This Thread
RE: How to pass a method as argument in an another method? - by anilanvesh - Sep-30-2021, 09:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question No disconnect method for snowflake.connector ? Calab 0 156 Jun-11-2024, 09:42 PM
Last Post: Calab
  Which method name would you choose? Gribouillis 7 468 May-30-2024, 07:05 AM
Last Post: buran
  Method returning None husksedgier 2 423 May-04-2024, 06:38 PM
Last Post: husksedgier
  Int.From_Bytes Method - Conversion of Non-Escaped Bytes Objects new_coder_231013 2 559 Apr-06-2024, 04:24 PM
Last Post: new_coder_231013
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 420 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  class definition and problem with a method HerrAyas 2 425 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  super() and order of running method in class inheritance akbarza 7 1,001 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  problem usage of static method akbarza 5 704 Feb-03-2024, 07:43 AM
Last Post: paul18fr
  Building a DoublyLinkedList in Python - - append method Drone4four 2 535 Jan-08-2024, 01:27 PM
Last Post: Drone4four
  How to pass encrypted pass to pyodbc script tester_V 0 995 Jul-27-2023, 12:40 AM
Last Post: tester_V

Forum Jump:

User Panel Messages

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