On Error Resume Next in Python

后端 未结 7 1131
栀梦
栀梦 2020-12-11 02:07

Snippet 1

do_magic() # Throws exception, doesn\'t execute do_foo and do_bar
do_foo()
do_bar()

Snippet 2

try:
    do_mag         


        
7条回答
  •  囚心锁ツ
    2020-12-11 02:33

    If all three functions accept same number of parameters:

    for f in (do_magic, do_foo, do_bar):
        try:
            f()
        except:
            pass
    

    Otherwise, wrap the function call with lambda.

    for f in (do_magic, lambda: do_foo(arg1, arg2)):
        try:
            f()
        except:
            pass
    

提交回复
热议问题