Reputation: 25
I'm using the internet to teach myself something useful like python. The resources I've been using so far 'Think python' by Downey and 'Learning Python' by Lutz. I was first introduced to python the Udacity programme on computer science, and have been teaching myself from here on in. I'm a coding neophyte and this is the first language I've attempted to learn.
My problem, is one posed by Downey in 'think python' and is defining a function that takes a function object as one of its arguments along with the number n.
It was introduced in the recursive programming chapter and has been giving me kittens.
My code so far:
def print_string(s):
print (s)
return
def do_n(f , n):
if n <=0:
return
f
do_n(f, n-1)
now I've been trying this with a simple predefined print function called print_ string that takes argument(s).
I save the script in a file called recursive.py and run it in IDLE then after it's run i call do_n with the following do_n(print_string('test'), 6)
I expect to print the sample string 'test' 6
times but it only prints once. There appears to be no recursion.
What am i doing wrong? Any help would be appreciated because i can't really afford to sign up with Udacity or anything else this is just me & the internet vs python. TIA
Upvotes: 0
Views: 136
Reputation: 25
Hi this is the final code that i ended up with that actually worked.
def print_string():
print ('result')
return
def do_n(f, n):
if n <=0:
return
print(type(f))
f()
print('been here')
do_n(f, n-1)
do_n(print_string, 6)
The two print calls 'been here' and type(f) were put in to try and understand what was going on with the program and don't need to be there, but i like the reminder of the leap i've made - so they're staying in.
Upvotes: 1
Reputation: 20344
I think you are very nearly there. I think the problem is with your call to f
in your method. Don't forget - you have passed in a function as f
, not a function call. So, when you have f
in your do_n
function, it does nothing. If you did f()
it would actually call f (with no arguments, which may or may not be allowed) and, possibly better, if you did f(n)
it should call print_string
with n
as the argument. I mocked up a little demo to get you going:
def print_string(arg1=''):
# mimicing your "pre-defined" function
# Note I've put a default value in arg1 so that you
# can call it without an argument to illustrate the priciple
print(arg1)
def do_n(f , n):
if n <=0:
return
f('test') # This is the line that was close, but not quite right
do_n(f, n-1)
do_n(print_string,10)
* EDIT *
In response to OP clarification in edit. The original code was as per the original post, but called with the line
do_n(print_string('test'),6)
The problem with this is that the print_string()
function is being before you ever enter do_n
. As the print_string()
function simply prints what it's given (I assume) and returns nothing - you're actually passing None
into the do_n
function.
As an aside and to convince yourself that the print_string
is being evaluated before do_n
is ever called - try making a single change to your original to say
do_n(print_string('test'),0)
You might expect to see nothing, but in fact you will see the output test
as the print_string is still called.
To pass a function into another function, you pass its name, but don't follow it with any brackets or arguments. In that way, you are passing the function object, not the result of calling that function.
You actually call the function (or indeed pass it on to another function) from within the receiving function where you want it to do something. My example code above still stands. Note - I pass in print_string
with no brackets or arguments.
This is a powerful technique - it might take a little thinking to get your head round, but it's worthwhile doing so. As a rule of thumb - if you see a function name that isn't followed by any brackets, it's being passed as a function object. If it's followed by brackets, it's being evaluated at that point.
Upvotes: 0