简体   繁体   中英

Unexpectable “except” raise in Python

Please help with python script. Python 2.7. I trying to make some function for repeating action with error check. So in function that I calling below (lib_func) as I think, there is no error. But "except" in repeater() raising any way.

If i don't use "x" in lib_func() - it works without error, but I still need put arguments in lib_func().

Sorry for bad english and thanks in advance for any help.

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))

output:

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

Process finished with exit code 0

Your indentation is wrong and repeater should be called as follows:

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

I dont understand what you want to achieve, but the above code executes lib_func(10) few times in a for loop.

Alternatively, you can use partial:

from functools import partial    
lf = partial(lib_func, 10)    
repeater(lf)

You should be passing the function pointer not calling it like you are here

repeater(lib_func(10))

It should be

repeater(lib_func)

You can modify it to take a number as argument

repeater(lib_func, 10)

Your function should be

def repeater(some_function, some_value):
    for attempts in range(0, 2):
        try:
            some_function(some_value)
        except:
            if attempts == 1:
                error_handler()
            else:
                continue
        #the break statement makes no sense 
    return #why would you return the function as it is not doing anything!

You have a problem of variable vs function.

repeater expects to be called with a function as parameter. So when you call repeater(lib_func) , all is fine : some_function() actually calls lib_func() .

But when you try to call repeater(lib_func(10)) , python first computes lib_func(10)) (returning None in above code) and then calls repeater(None) => giving exception because None is not callable !

If you want to be able to call repeater with a function with one argument, you should pass the argument to repeater . For example :

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

repeater(lib_func, 10)

Or if you want to accept a variable number of arguments :

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

repeater(lib_func, 10)

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