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 16:15 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.
From: Alister on 24 May 2010 16:45 On Mon, 24 May 2010 13:15:01 -0700, joy99 wrote: > 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. I don't quite see the point of your functions as they do not use their parameters in any way, but you need to set a return value IDLE 2.6.5 >>> def function1(n): element1=5 element2=6 add=element1+element2 return add >>> def function2(n): element3=7 element4=22 mult=element3*element4 return mult >>> def function3(x,y): print "add= %s mult=%s" % (x , y) >>>function3(function1(1),funtcion2(2)) -- Your digestive system is your body's Fun House, whereby food goes on a long, dark, scary ride, taking all kinds of unexpected twists and turns, being attacked by vicious secretions along the way, and not knowing until the last minute whether it will be turned into a useful body part or ejected into the Dark Hole by Mister Sphincter. We Americans live in a nation where the medical-care system is second to none in the world, unless you count maybe 25 or 30 little scuzzball countries like Scotland that we could vaporize in seconds if we felt like it. -- Dave Barry, "Stay Fit & Healthy Until You're Dead"
From: Vlastimil Brom on 24 May 2010 16:56 2010/5/24 joy99 <subhakolkata1234(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
From: Alister on 24 May 2010 17:08 On Mon, 24 May 2010 22:56:34 +0200, Vlastimil Brom wrote: > 2010/5/24 joy99 <subhakolkata1234(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 I did not realise you could pass a function like this, I am sure it could lead to some interesting programming. I am still new to OOP & the light is only just starting to switch on. analysing your examples I now realise that all function parameters are Objects & EVERYTHING(well almost anyway) is an object. It just goes to show that even when you know 1 answer to the original question You can still learn by looking at others -- "And, of course, you have the commercials where savvy businesspeople Get Ahead by using their MacIntosh computers to create the ultimate American business product: a really sharp-looking report." -- Dave Barry
From: joy99 on 24 May 2010 17:39
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 Vlastimil, You are right. Your approach will do to me. I was trying Python Doc either I do not know where to check, or I could not find. I am trying to work out. Numbers I can pass, I was checking the last example in Python Docs with "def cheeseshop(kind, *arguments, **keywords):" if that gives me any clue. If you can kindly try. Best Regards, Subhabrata. |