Python中不可思议的"除外"加注

Evg*_*eny 2 python wrapper python-2.7

请帮助python脚本.Python 2.7.我尝试使用错误检查重复操作的一些功能.所以在我认为的函数中我调用下面(lib_func),没有错误.但是在转发器()中的"除了"以任何方式提升.

如果我不在lib_func()中使用"x" - 它可以正常工作,但我仍然需要在lib_func()中输入参数.

对不起英语不好,并提前感谢您的帮助.

def error_handler():
    print ('error!!!!!!')

def lib_func(x):
    print ('lib_func here! and x = ' + str(x))

def repeater(some_function):
    for attempts in range(0, 2):
        try:
            some_function()
        except:
            if attempts == 1:
                error_handler()
            else:
                continue
    break
return some_function

repeater(lib_func(10))
Run Code Online (Sandbox Code Playgroud)

输出:

lib_func here! and x = 10
error!!!!!!

Process finished with exit code 0
Run Code Online (Sandbox Code Playgroud)

Mar*_*cin 6

您的缩进是错误的,应该按如下方式调用转发器:

def repeater(some_function):
    for attempts in range(0, 2):
        try:
            some_function()
        except:
            if attempts == 1:
                error_handler()
            else:
                continue
    return some_function()

repeater(lambda: lib_func(10)) # pass `lib_func(10)` using lambda
Run Code Online (Sandbox Code Playgroud)

我不明白你想要实现什么,但上面的代码lib_func(10)在for循环中执行了几次.

或者,您可以使用partial:

from functools import partial    
lf = partial(lib_func, 10)    
repeater(lf)
Run Code Online (Sandbox Code Playgroud)