python在函数内部调用函数 [英] python calling a function inside a function

查看:234
本文介绍了python在函数内部调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CS的初学者,我一直在尝试自己编写一本Python书.

I'm a beginner to CS, and I've been trying to work through a Python book on my own.

我目前正在递归,但是我有点卡住了.

I'm currently on recursion, but I'm a bit stuck.

该练习要求我编写一个名为do_n的函数,该函数以一个函数对象和一个数字n作为参数,并调用给定的函数n次.

The exercise asks for me to write a function called do_n that takes a function object and a number, n, as arguments, and that calls the given function n times.

这是我的代码

def countdown(n):
    if n<= 0:
        print 'Blastoff'
    return
else:
    print n
    countdown(n-1)

def do_n(f(n), x):
    if x<=0:
        return
    else:
        f(n)
        do_n(f, x-1)

do_n(countdown(3), 3)

执行此操作时,由于def do_n(f(n),x)中的语法无效而出现错误.如果我将其更改为

When I do this, there's an error because of invalid syntax in def do_n(f(n), x). If I change it to

def do_n(f, x):
    if x<=0:
        return
else:
    f(n)
    do_n(f, x-1)

出现错误,因为在else语句中未定义n.

There is an error because n is not defined in the else statement.

我正试图弄清楚如何使这项工作..谢谢大家!

I am trying to figure out how to make this work.. thanks guys!

推荐答案

这是一个棘手的问题.

基本上,解决方案是:

def do_n(f, n):
    if n <= 0:
        return
    f(n)
    do_n(f, n-1)

但是,如果您实际上尝试将其与print_n结合使用,那么一切都将变得一团糟.

However, if you actually try combining it with print_n then all hell breaks loose.

首先,我们必须在上面的代码中再添加一个参数s,以便能够将要打印的任何字符串传递n次.

First of all, we have to add one more argument, s, to the above code to be able to pass any string that we want to print out n times.

第二,上面的do_n应该能够将任何传递的函数重复n次,只要该函数不会干扰do_n即可.不幸的是,这正是这里发生的情况.

Second of all, the above do_n should be able to repeat any passed function n times so long as the function doesn't mess with do_n. Unfortunately, this is exactly what happens here.

这本书的分配似乎很清楚:"...编写一个名为do_n的函数,该函数接受一个函数对象和一个数字n作为参数,并调用给定函数n次..."换句话说,do_n调用传递给它的任何函数n次.您可能会认为,如果我们传递s ='Hello'并且n = 3,那么do_n(f,s,n)应该输出'Hello'9次...因为print_n('Hello',3)会输出'Hello'3次并且do_n(print_n,'Hello',3)应该使结果翻三倍...

The assignment from the book seems clear: "...write a function called do_n that takes a function object and a number, n, as arguments, and that calls the given function n times..." or in other words do_n calls any function passed to it n times. You'd think if we pass s = 'Hello' and n = 3 then do_n(f, s, n) should output 'Hello' 9 times... because print_n('Hello', 3) prints 'Hello' 3 times and do_n(print_n, 'Hello', 3) is supposed to triple that result... gotcha.

实际发生的是在第一次递归中,do_n确实打印了3次"Hello",因为print_n如此说.但是在下一个实例中,do_n仅打印两次"Hello",因为它在前一个实例中减去了3-1,现在又减去了print_n('Hello',2).在最后一个实例中,出于相同的原因,do_n仅打印一次"Hello".因此,"Hello"输出的总数为6.

What actually happens is in the first instance of recursion do_n does indeed print out 'Hello' 3 times, because print_n says so. But in the next instance do_n prints out 'Hello' only two times, because it has subtracted 3-1 in the previous instance and now print_n('Hello', 2). In the last instance do_n prints 'Hello' only once, for the same reason. So the total amount of 'Hello' output is 6.

最后,如果使用FOR LOOP而不是递归来定义do_n,则实际上将获得9个输出的正确结果:

Finally, if you use FOR LOOP instead of recursion to define do_n, you will actually get the correct result of 9 outputs:

def print_n(s, n):
    if n <= 0:
        return
    print(s)
    print_n(s, n-1)

def do_n(f, s, n):
    for i in range(n):
        f(s, n)

do_n(print_n, 'Hello', 3)

P.S.在Think Python 2E的第52页上,我们鼓励对循环使用递归:

P.S. On Page 52 of Think Python 2E we are encouraged to use recursion over for loop:

对于这样的简单示例,使用for循环可能更容易.但是稍后我们将看到难以通过for循环编写且易于通过递归编写的示例,因此尽早开始是个好方法."

"For simple examples like this, it is probably easier to use a for loop. But we will see examples later that are hard to write with a for loop and easy to write with recursion, so it is good to start early."

但是,在此特定练习中,使用递归会强制输出与赋值冲突,因为do_n和print_n相互矛盾,因为在递归的帮助下构造do_n会错误地减小n.

However, in this particular exercise, using recursion forces an output which clashes with the assignment, seeing that do_n and print_n are at odds with each other because constructing do_n with the help of recursion wrongly reduces n in each instance.

我做对了吗?

这篇关于python在函数内部调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆