Prev: Most reliable/pythonic way to tell if an instance comes from a class implemented in C/etc?
Next: Most reliable/pythonic way to tell if an instance comes from a class implemented in C/etc?
From: joy99 on 24 May 2010 18:08 On May 25, 1:56 am, Vlastimil Brom <vlastimil.b...(a)gmail.com> wrote: > 2010/5/24 joy99 <subhakolkata1...(a)gmail.com>: > > > > > > > > > Dear Group, > > > I have a small question on function. > > > If I write two functions like the following: > > > IDLE 2.6.5 > >>>> def function1(n): > > element1=5 > > element2=6 > > add=element1+element2 > > print "PRINT THE ADDITION",add > > >>>> def function2(n): > > element3=7 > > element4=22 > > mult=element3*element4 > > print "PRINT THE MULTIPLICATION",mult > > > Can I now write a third function where the above two functions can be > > passed as argument or parameter? > > > Best Regards, > > Subhabrata. > > > NB: As I copied the code from IDLE to MS-Word befor posting here, > > codes may have slight indentation errors. > > -- > >http://mail.python.org/mailman/listinfo/python-list > > Hi, > while it is quite unclear to me, what you are trying to achieve (what > are the passed n arguments supposed to do?), you can well pass an > already defined function as an argument to another function; if you > want to select a function for the needed operation, if can be e.g.: > > def compute(arg1, arg2, fn): > fn(arg1, arg2) > > - supposing you don't want to "return" the result but just print it as > your functions do; > is it what you were after or did I miss something more complex? > > hth > vbr- Hide quoted text - > > - Show quoted text - Dear Vlastimir, As pointed out by Alister, I can print the values of function1 and function2 with the help of another function3, but my target is to call the "add" value of function1 and "mult" value of function2 in a third function or the values and parameters of the third function in fourth function. While calling, I am looking not only to print, but to use them or manipulate them. I hope I am bit clear now. Best Regards, Subhabrata.
From: Kushal Kumaran on 25 May 2010 02:30 On Tue, May 25, 2010 at 3:38 AM, joy99 <subhakolkata1234(a)gmail.com> wrote: > <snip> > > Dear Vlastimir, > > As pointed out by Alister, I can print the values of function1 and > function2 with the help of another function3, but my target is to call > the "add" value of function1 and "mult" value of function2 in a third > function or the values and parameters of the third function in fourth > function. While calling, I am looking not only to print, but to use > them or manipulate them. > > I hope I am bit clear now. > If you need to use the values in another function, you need a way to let that function get its hands on the values. Your function1 and function2 should return the values they compute instead of printing them out. -- regards, kushal
From: Peter Otten on 25 May 2010 03:18 Kushal Kumaran wrote: > On Tue, May 25, 2010 at 3:38 AM, joy99 <subhakolkata1234(a)gmail.com> wrote: >> <snip> >> >> Dear Vlastimir, >> >> As pointed out by Alister, I can print the values of function1 and >> function2 with the help of another function3, but my target is to call >> the "add" value of function1 and "mult" value of function2 in a third >> function or the values and parameters of the third function in fourth >> function. While calling, I am looking not only to print, but to use >> them or manipulate them. >> >> I hope I am bit clear now. >> > > If you need to use the values in another function, you need a way to > let that function get its hands on the values. Your function1 and > function2 should return the values they compute instead of printing > them out. For example: >>> def add(x, y): .... return x + y .... >>> def mul(x, y): .... return x * y .... >>> def sum_of_squares(x, y): .... return add(mul(x, x), mul(y, y)) .... >>> sum_of_squares(3, 4) 25 OP: If that addresses your question I suggest that you work through some introductory text about python. The python wiki has a few suggestions, see http://wiki.python.org/moin/BeginnersGuide Peter
From: joy99 on 27 May 2010 16:50
On May 25, 12:18 pm, Peter Otten <__pete...(a)web.de> wrote: > Kushal Kumaran wrote: > > On Tue, May 25, 2010 at 3:38 AM,joy99<subhakolkata1...(a)gmail.com> wrote: > >> <snip> > > >> Dear Vlastimir, > > >> As pointed out by Alister, I can print the values of function1 and > >> function2 with the help of another function3, but my target is to call > >> the "add" value of function1 and "mult" value of function2 in a third > >> function or the values and parameters of the third function in fourth > >> function. While calling, I am looking not only to print, but to use > >> them or manipulate them. > > >> I hope I am bit clear now. > > > If you need to use the values in another function, you need a way to > > let that function get its hands on the values. Your function1 and > > function2 should return the values they compute instead of printing > > them out. > > For example: > > >>> def add(x, y): > ... return x + y > ...>>> def mul(x, y): > > ... return x * y > ...>>> def sum_of_squares(x, y): > > ... return add(mul(x, x), mul(y, y)) > ...>>> sum_of_squares(3, 4) > > 25 > > OP: If that addresses your question I suggest that you work through some > introductory text about python. The python wiki has a few suggestions, see > > http://wiki.python.org/moin/BeginnersGuide > > Peter Hi Peter and Other Kind Contributors of the Group, I got lot of insights from the discussion in the group. Though I did not get the exact answer but from Peter's link I could work them out. Thank you, Peter, for kindly referring me the link. I like to thank others of the group also for kindly spending their valuable time. I wanted to define one function and call the value of that function to be used by another function. If I misquoted the problem, I am sorry. Wishing You all a Great Day Ahead, Best Regards, Subhabrata. |