Mailing List Archive

How to pass a method as argument?
I want to write a python calculator program that has different methods to add, subtract, multiply which takes 2 parameters. I need to have an execute method when passed with 3 parameters, should call respective method and perform the operation. How can I achieve that?



class calc():
def __init__(self,a,b):
self.a=a
self.b=b

def ex(self,fun):
self.fun=fun
if fun=="add":
self.add()

def add(self):
return self.a+self.b
def sub(self):
return self.a-self.b
def mul(self):
return self.a*self.b
def div (self):
return self.a/self.b
def execu(
obj1=calc()
obj1.execu("add",1,,2)
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to pass a method as argument? [ In reply to ]
Il giorno giovedì 30 settembre 2021 alle 07:11:28 UTC+2 anila...@gmail.com ha scritto:
> I want to write a python calculator program that has different methods to add, subtract, multiply which takes 2 parameters. I need to have an execute method when passed with 3 parameters, should call respective method and perform the operation. How can I achieve that?
>
>
>
> class calc():
> def __init__(self,a,b):
> self.a=a
> self.b=b
>
> def ex(self,fun):
> self.fun=fun
> if fun=="add":
> self.add()
>
> def add(self):
> return self.a+self.b
> def sub(self):
> return self.a-self.b
> def mul(self):
> return self.a*self.b
> def div (self):
> return self.a/self.b
> def execu(
> obj1=calc()
> obj1.execu("add",1,,2)

For example:

| def ex(self, fun):
| getattr(self, fun)()
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to pass a method as argument? [ In reply to ]
Please help me to out from this difficulties, whenever I'm trying to
install python idle it is showing me the error called**** "[1]python.exe -
System Error"******
I have tried couple of ways to get rid out of this but failed every time,
please please help me to find the exact solution for this problem. Thank
You
Sent from OPPO Mail
On Edmondo Giovannozzi <edmondo.giovannozzi@gmail.com>, 30 Sep 2021 20:34
wrote:

References

Visible links
1. http://python.exe/
--
https://mail.python.org/mailman/listinfo/python-list
RE: How to pass a method as argument? [ In reply to ]
Sounds like an excellent homework question.

But your method of using an object is not what first comes to mind based on
your cursory description.

There is a python idiom using functional programming that looks like this:

def doit(a, b, fun): return(fun(a,b))

So make up your own functions like:

def addit(a,b): return(a+b)

And make up others doing multiply and whatever and call something like this:

doit(3,5, addit)

Now if you want a user to put in text like "add" that is another story. If
you want this in the context of an object, ditto.


-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net@python.org> On
Behalf Of Anil Anvesh
Sent: Thursday, September 30, 2021 1:11 AM
To: python-list@python.org
Subject: How to pass a method as argument?

I want to write a python calculator program that has different methods to
add, subtract, multiply which takes 2 parameters. I need to have an execute
method when passed with 3 parameters, should call respective method and
perform the operation. How can I achieve that?



class calc():
def __init__(self,a,b):
self.a=a
self.b=b

def ex(self,fun):
self.fun=fun
if fun=="add":
self.add()

def add(self):
return self.a+self.b
def sub(self):
return self.a-self.b
def mul(self):
return self.a*self.b
def div (self):
return self.a/self.b
def execu(
obj1=calc()
obj1.execu("add",1,,2)
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: How to pass a method as argument? [ In reply to ]
On 30/09/2021 18.11, Anil Anvesh wrote:
> I want to write a python calculator program that has different methods to add, subtract, multiply which takes 2 parameters. I need to have an execute method when passed with 3 parameters, should call respective method and perform the operation. How can I achieve that?
>
>
>
> class calc():
> def __init__(self,a,b):
> self.a=a
> self.b=b
>
> def ex(self,fun):
> self.fun=fun
> if fun=="add":
> self.add()
>
> def add(self):
> return self.a+self.b
> def sub(self):
> return self.a-self.b
> def mul(self):
> return self.a*self.b
> def div (self):
> return self.a/self.b
> def execu(
> obj1=calc()
> obj1.execu("add",1,,2)
>

This is a common course-assignment.

NB the code-examples below are incomplete. You may need to research
these techniques further, and thus learn how 'it' all fits-together
(I set 'homework' rather than doing it!)


There are several techniques which can be employed and/or combined here.
1 if-elif 'ladder'
2 dict[ionary] as select/case construct
3 functions as 'first-class objects'

The basic problem is to take a 'command' which is formatted as a
str[ing], and decide which of several options to choose:

1 if-elif is a simple and easy-to-read solution:

if command == "add":
do_this...
elif command == "sub":
do_that...
...
else:
# oops: "I'm afraid I can't do that, Dave"
# don't forget to add a 'catch-all' to handle user-error!

2 using a dict is shorter and avoids some of the 'boiler-plate'
repetition which causes many of us to rebel against using the above. We
use the command received from the user as a key into the dict.

3 a function and/or its name is as much data as the 'variable' "command"
or indeed the str-constant "add" - once it has been defined! Thus it is
possible to treat functions like anything else:

data_as_string = "my string"
# can be likened to:-
def my_function(): pass
data_as_function = my_function

Note that (like most else), the function must be defined before it can
be 'used'!

Returning to the stated-problem:

def add( etc ): ...

then using the if-elif 'ladder' above as a framework create a dict which
'links' the input command (as str) to the applicable function:

calculator = { "add": add,
"sub": sub,
# indeed we can become rather more 'creative'
"+" " add,
...
}

Thereafter, we can apply the dict to solve the problem:

calculator.get( command,
"Error message/advice, eg mentioning add, or + etc"
)


NB it has been left to you to perfect the technique so that the value(s)
to be calculated are properly communicated to the chosen function.

PS you may find the Python-Tutor Discussion List helpful
--
Regards,
=dn
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to pass a method as argument? [ In reply to ]
On 9/29/21 23:11, Anil Anvesh wrote:
> I want to write a python calculator program that has different methods to add, subtract, multiply which takes 2 parameters. I need to have an execute method when passed with 3 parameters, should call respective method and perform the operation. How can I achieve that?
>


let me add - this is probably not the place you are in your Python
learning, so don't worry about this, but the operator module is designed
for these kind of usages, when you want to pass an operator like + -
etc. but of course can't pass the op itself to take actions because it's
not a callable - the operator module has callables that can be used.


--
https://mail.python.org/mailman/listinfo/python-list
Re: How to pass a method as argument? [ In reply to ]
On Friday, October 1, 2021 at 6:04:34 AM UTC+5:30, Mats Wichmann wrote:
> On 9/29/21 23:11, Anil Anvesh wrote:
> > I want to write a python calculator program that has different methods to add, subtract, multiply which takes 2 parameters. I need to have an execute method when passed with 3 parameters, should call respective method and perform the operation. How can I achieve that?
> >
> let me add - this is probably not the place you are in your Python
> learning, so don't worry about this, but the operator module is designed
> for these kind of usages, when you want to pass an operator like + -
> etc. but of course can't pass the op itself to take actions because it's
> not a callable - the operator module has callables that can be used.

I solved it with simple if condition and without using init

#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)
--
https://mail.python.org/mailman/listinfo/python-list