简体   繁体   中英

How do I use recursion to write a function that calls another function repeatedly in Python?

I am trying to write a function that calls another function repeatedly. This is what I have so far:

def function1(n, function_object):
    if n > 0:
        function_object
        function1(n-1, function_object)

def function2(x, word):
    for i in range(x):
        print word

y = 'pluto'

function1(3, function2(3, y))

This is what I get when I try to run it:

>>> 
pluto
pluto
pluto
>>> 

I thought it would print 'pluto' nine times, but it seems that function2 is only executing once. How can I get it to execute repeatedly inside function1?

  • missed () after function_object.
  • Call to function1 is passing the return value of function2 , you should pass function object. I replaced it with lambda in the following code. (to make a anonymous function that call function2 instead of calling function2 before calling function1 )

def function1(n, function_object):
    if n > 0:
        function_object() # <---- Added ()
        function1(n-1, function_object)

def function2(x, word):
    for i in range(x):
        print word

y = 'pluto'

function1(3, lambda: function2(3, y)) # <---

In your code, function1 was calling itself AND another function. It doesn't need to be that complicated.

Also, in the last line you were passing function2(3, y) as a parameter - because you have parens () there, that calls function2 and gets the result, then passes the result to function1. I don't think that's what you meant to do.

So I've modified it. As you requested, here is a function (function1) which calls another (arbitrary) function N times, passing the current value of N along with arbitrary arguments.

def function1(n, function_object, *args):
    while n > 0:
        function_object(n, *args)
        n -= 1

def function2(n, x, word):
    for i in range(x):
        print "%s, %d, %d" % (word, n, i)

y = 'pluto'

function1(3, function2, 3, y)

Output:

pluto, 3, 0
pluto, 3, 1
pluto, 3, 2
pluto, 2, 0
pluto, 2, 1
pluto, 2, 2
pluto, 1, 0
pluto, 1, 1
pluto, 1, 2

When you do this:

function2(3, y)

that function is run immediately , and its return value (which is None in this case) is passed into function1 as the function_object argument. In function1 , you try to call it like this:

function_object

but that doesn't actually call it. So, function2 will end up being called a total of once - before function1 is ever called.

If you replace that with function_object() , it does call it - but then you get an error, since function_object is None , which isn't callable. To pass function2 in instead of None , just don't call it outside yet - instead you need to do this:

function1(3, function2)

This will get you an error saying that function2 needs to take two arguments, but you're not giving it any . So the next step is to work out how to pass those arguments to it - which needs to happen at the time it is called. But function1 expects a function with no arguments.. you can fix that problem by writing another function that takes no arguments, and calls function2 with the two arguments you want, and pass that into function1. So it looks like this:

def function1(n, function_object):
    if n > 0:
        function_object()
        function1(n-1, function_object)

def function2(x, word):
    for i in range(x):
        print word

y = 'pluto'

def function3():
    function2(3, y)

function1(3, function3)

If you're only using this function3 once, and there isn't a sensible name you can give it, you can use a lambda to create an anonymous function that does the same job:

def function1(n, function_object):
    if n > 0:
        function_object()
        function1(n-1, function_object)

def function2(x, word):
    for i in range(x):
        print word

y = 'pluto'
function1(3, lambda: function2(3, y))

在这种情况下,Lambda是一个不错的选择,否则,您可以看看这篇文章,他们解释另外两种实现方法:将带有参数的函数传递给Python中的另一个函数?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM