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?
#7
If you define the math functions you may as well make them generally useful. I would do something like this:
#calculator class with arithmetic methods

class calc():
    def solve(self, equation):
        '''Solve eqation in the form a op b where b is in "+-*/"'''
        a, op, b = equation.split()
        print(equation, '=', self.execute(op, float(a), float(b)))

    def execute(self, op, a, b):
        '''Return result of binary math operation'''
        if op == "+":
            return self.add(a, b)
        if op == "-":
            return self.sub(a, b)
        if op == "*":
            return self.mul(a, b)
        if op == "/":
            return self.div(a, b)
        raise ValueError(f'{op} is not a recognized operator')

    def add(self, a, b):
        '''Return a + b'''
        return a + b

    def sub(self, a, b):
        '''Return a - b'''
        return a - b

    def mul(self, a, b):
        '''Return a * b'''
        return a * b

    def div(self, a, b):
        '''Return a / b'''
        return a / b

cal = calc()
cal.solve('6 / 3')
print(cal.execute('*', 2, 3))
If we pretend that the calculator has support for more interesting operations, making those operations visible outside the calculator changes it from an application to a library. Your code exposed the operations, but not in a generically useful way. If I am using your library I probably want the div function to return the quotient instead of None.

Though this is a silly program it aspires to do "good design" things like separating the implementation from the interface. add(a, b) knows how to compute a + b. It should not have to know about how results are displayed (there should not be a print). execute(op, a, b) does not know how to do any operations, but it knows how to find the right function for the operator. solve(equation) does not know anything about math, but it knows how to parse an equation and display the results. When you design code you should look for this kind of division of labor. Every function should do one thing that is easily described with a sentence or two. If you cannot describe what a function does you need to "refactor" your code to have functions that are well defined.
Reply


Messages In This Thread
RE: How to pass a method as argument in an another method? - by deanhystad - Sep-30-2021, 10:18 PM

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