可以使用Python try/ except 函数来规则所有这些(使用不同的输入)吗?
我试图在 Python 中定义一个可以处理各种其他函数的 try/ except 函数。 这个想法就是这样的:
def func1(x,y):
z = x+y
return z
def error_check(func1):
try: stuff = func1()
except:
print('Error')
return stuff
error_check(func1('1',2))
在这种情况下,对于这些输入,error_check 函数甚至不起作用。 目的是我可以将 error_check 与另一个具有不同数量输入的 func2 一起使用。例如:
def func2(w,x,y):
z = w+x+y
return z
这可能吗?
多谢
I'm trying to define in Python a try/except function that can handle various other function.
The idea is quite this one:
def func1(x,y):
z = x+y
return z
def error_check(func1):
try: stuff = func1()
except:
print('Error')
return stuff
error_check(func1('1',2))
In this case, with those inputs, the error_check function doesn't even work.
The aim is that I can use error_check with another func2 with a diverse number of inputs. For examples:
def func2(w,x,y):
z = w+x+y
return z
Is that possible?
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
整个想法的核心问题是捕获错误并不意味着您修复了错误。只需使用引发异常的
func1
尝试您的函数,您就会看到问题:请注意,我们在输出中看不到
Computing the answer...
,而我们的函数不返回 42! 我们在error_check
中无法做任何事情来改变这一点。问题是,如果
func1
引发异常,它不会返回任何内容。因此没有东西
可以返回。您不能通过忽略函数引发的异常来强制函数“工作”;如果一个函数引发异常,则意味着它无法完成它应该做的事情。也许您对此表示同意,并希望您的
error_check
返回None
而不是func1
应该返回的内容。如果是这样,您可以这样做:如果出现异常,这将隐式返回
None
。它不会引发另一个异常,但func1
仍然不会做它应该做的事情:请注意,如果我们需要
42
值来做其他事情在我们的程序中,那个现在可能会引发错误,如果您捕获该错误,那么它之后的事物将会引发错误,依此类推,并且很快。最终,仅仅发现错误并忽略它们不会给自己带来任何好处。话虽如此,如果您希望能够将其他参数传递给
func1
,请使用*args
和**kwargs
:现在您可以这样做:
和
func1
将在error_check
内调用,*args
为('1', 2)
。The core problem with this whole idea is that catching an error doesn't mean you fixed the error. Just try your function with a
func1
that raises an exception and you'll see the problem:Note that we don't see
Computing the answer...
in the output, and our function does not return 42! There is nothing we can do inerror_check
that will change this.The problem is that if
func1
raises an exception, it does not return anything. Hence there is nostuff
to return. You can't force a function to "work" by just ignoring the exception it raises; if a function raises an exception, it means it could not do the thing it was supposed to do.Maybe you're okay with this and want your
error_check
to returnNone
instead of whateverfunc1
was supposed to return. If so, you could do:This will implicitly return
None
if there's an exception. It won't raise another exception, butfunc1
is still not going to do the thing that it was supposed to do:Note that if we needed that
42
value for something else in our program, that thing is now probably going to raise an error, and if you catch that error, the thing after it will raise an error, and so on, and so on. Ultimately, you don't do yourself any favors by just catching errors and ignoring them.All that said, if you want to be able to pass other arguments to
func1
, use*args
and**kwargs
:Now you can do:
and
func1
will be called withinerror_check
with*args
of('1', 2)
.您的问题是,在最后一行中,您实际上是在调用
func1
,然后对结果调用error_check
。请尝试这样的操作,其中lambda
会延迟求值,直到try
块出现,就像我们想要的那样:您还会遇到一个错误,因为
stuff
未定义如果try
块失败,那么我就冒昧地修复了这个问题。Your issue is that in the last line there, you are actually calling
func1
and then callingerror_check
on the result. Try something like this instead, where thelambda
delays evaluation until thetry
block like we want:You also have an error in that
stuff
is undefined if thetry
block failed, so I took the liberty of fixing that.