Re: [Tutor] Passing functions(with parameters) as paramiters to (looping)functions.
On 08/16/2011 01:46 PM, Peter Otten wrote: Jeff Peters wrote: Hi; I am trying to run a function inside a continuing loop, but do not seem to be able to pass any parameters (arguments ) when I do so. I have placed working and non-working code , with output below. ## This works: def loop(fn ): for i in range(5): fn( ) def this_function(a=" i am not a string"): print( a ) loop(this_function) ## with output: >>> i am not a string i am not a string i am not a string i am not a string i am not a string >>> ## But , this does not : def loop(fn ): for i in range(5): fn( ) def this_function(a=" i am not a string"): print( a ) loop(this_function("I am a string") ) ## note the only change is here You are calling this_function() and then pass the result of the function call to your other function loop(). Instead you need another function that builds a function that calls this_function() with the desired argument: def loop(f): ... for i in range(5): ... f() ... def this_function(a): ... print(a) ... def make_function(f, arg): ... def g(): ... f(arg) ... return g ... loop(make_function(this_function, "foo")) foo foo foo foo foo loop(make_function(this_function, "bar")) bar bar bar bar bar Of course you could also change loop() to pass on arbitrary arguments: def loop(f, *args, **kw): ... for i in range(3): ... f(*args, **kw) ... loop(print, 1, 2) 1 2 1 2 1 2 loop(print, 1, 2, sep="<-->") 1<-->2 1<-->2 1<-->2 Because building a function that just calls another function with some predefined arguments is a common need the standard library has functools.partial(): from functools import partial print42 = partial(print, 42) print42() 42 loop(print42) 42 42 42 Another variant is a lambda function with a default argument: loop(lambda a="whatever": print(a)) whatever whatever whatever ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Thanks, that is what I needed. not splitting the function name and argument list was my problem. and the function creator idea really appeals thanks again - jeff ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Passing functions(with parameters) as paramiters to (looping)functions.
>def loop(fn ): > for i in range(5): > fn( ) > >def this_function(a=" i am not a string"): > print( a ) > >loop(this_function("I am a string") ) ## note the only change is here > >## With this as output: > > >>> >I am a string >Traceback (most recent call last): > File "/home/jeff/MyPythonStuff/call_sub.py", line 9, in > loop(this_function("I am a string") ) > File "/home/jeff/MyPythonStuff/call_sub.py", line 4, in loop > fn( ) >TypeError: 'NoneType' object is not callable > >>> NOTE: All code is untested. You get a NoneType because this_function returns a None. What is happening is the this_function("xxx") gets called first and then that return value gets passed into loop as 'loop(None)'. I am not sure exactly what you are trying to do, but I would probably do something like passing in a list of arguments. loop(this_function, iterable_of_arguments) def loop(fn, args): for arg in args : fn( arg ) The way I would get an argument gets passed to this_function is really dependent on your goal. You may want to look at itertools / map libraries as well for more options. Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Passing functions(with parameters) as paramiters to (looping)functions.
Jeff Peters wrote: > Hi; > > I am trying to run a function inside a continuing loop, but do not seem > to be able to pass any parameters (arguments ) when I do so. > I have placed working and non-working code , with output below. > > ## This works: > > def loop(fn ): > for i in range(5): > fn( ) > > def this_function(a=" i am not a string"): > print( a ) > > loop(this_function) > > ## with output: > >>> > i am not a string > i am not a string > i am not a string > i am not a string > i am not a string > >>> > > ## But , this does not : > > def loop(fn ): > for i in range(5): > fn( ) > > def this_function(a=" i am not a string"): > print( a ) > > loop(this_function("I am a string") ) ## note the only change is here You are calling this_function() and then pass the result of the function call to your other function loop(). Instead you need another function that builds a function that calls this_function() with the desired argument: >>> def loop(f): ... for i in range(5): ... f() ... >>> def this_function(a): ... print(a) ... >>> def make_function(f, arg): ... def g(): ... f(arg) ... return g ... >>> loop(make_function(this_function, "foo")) foo foo foo foo foo >>> loop(make_function(this_function, "bar")) bar bar bar bar bar >>> Of course you could also change loop() to pass on arbitrary arguments: >>> def loop(f, *args, **kw): ... for i in range(3): ... f(*args, **kw) ... >>> loop(print, 1, 2) 1 2 1 2 1 2 >>> loop(print, 1, 2, sep="<-->") 1<-->2 1<-->2 1<-->2 Because building a function that just calls another function with some predefined arguments is a common need the standard library has functools.partial(): >>> from functools import partial >>> print42 = partial(print, 42) >>> print42() 42 >>> loop(print42) 42 42 42 Another variant is a lambda function with a default argument: >>> loop(lambda a="whatever": print(a)) whatever whatever whatever ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Passing functions(with parameters) as paramiters to (looping)functions.
On Tue, Aug 16, 2011 at 1:44 PM, Jeff Peters wrote: > Hi; > > I am trying to run a function inside a continuing loop, but do not seem to > be able to pass any parameters (arguments ) when I do so. > I have placed working and non-working code , with output below. > > ## This works: > > def loop(fn ): >for i in range(5): >fn( ) > > def this_function(a=" i am not a string"): >print( a ) > > loop(this_function) > > ## with output: > >>> > i am not a string > i am not a string > i am not a string > i am not a string > i am not a string > >>> > > ## But , this does not : > > def loop(fn ): >for i in range(5): >fn( ) > > def this_function(a=" i am not a string"): >print( a ) > > loop(this_function("I am a string") ) ## note the only change is here > > ## With this as output: > > >>> > I am a string > Traceback (most recent call last): > File "/home/jeff/MyPythonStuff/**call_sub.py", line 9, in >loop(this_function("I am a string") ) > File "/home/jeff/MyPythonStuff/**call_sub.py", line 4, in loop >fn( ) > TypeError: 'NoneType' object is not callable > >>> > Your loop() function expects the parameter 'fn' to be a function that it can then call. In your second example, it doesn't work because you already called the function and is passing as parameter 'fn' whatever this_function() returned. >>> type(this_function) >>> type(this_function("I am a string")) I am a string If you really want this design, one way to "fix" it is to change loop() to accept another argument that is going to be passed to the function. def loop(fn, b): for i in range(5): fn(b) Perhaps if you explain what you are trying to accomplish, someone can suggest a better way to design the code. -- Giovanni Tirloni sysdroid.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Passing functions(with parameters) as paramiters to (looping)functions.
Hi; I am trying to run a function inside a continuing loop, but do not seem to be able to pass any parameters (arguments ) when I do so. I have placed working and non-working code , with output below. ## This works: def loop(fn ): for i in range(5): fn( ) def this_function(a=" i am not a string"): print( a ) loop(this_function) ## with output: >>> i am not a string i am not a string i am not a string i am not a string i am not a string >>> ## But , this does not : def loop(fn ): for i in range(5): fn( ) def this_function(a=" i am not a string"): print( a ) loop(this_function("I am a string") ) ## note the only change is here ## With this as output: >>> I am a string Traceback (most recent call last): File "/home/jeff/MyPythonStuff/call_sub.py", line 9, in loop(this_function("I am a string") ) File "/home/jeff/MyPythonStuff/call_sub.py", line 4, in loop fn( ) TypeError: 'NoneType' object is not callable >>> My OS is Debian 64 bit I get the same output for both python2.7 and Python3.1 I think this should be do-able but I am in need of a clue. Thanks. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor