Suppose I have a function definiton:
假设我有一个函数定义:
def test():
print 'hi'
I get a TypeError whenever I gives an argument.
每当我给出一个参数时,我得到一个TypeError。
Now, I want to put the def statement in try. How do I do this?
现在,我想把def语句放在try中。我该怎么做呢?
6 个解决方案
#1
try:
test()
except TypeError:
print "error"
#2
In [1]: def test():
...: print 'hi'
...:
In [2]: try:
...: test(1)
...: except:
...: print 'exception'
...:
exception
Here is the relevant section in the tutorial
这是教程中的相关部分
By the way. to fix this error, you should not wrap the function call in a try-except. Instead call it with the right number of arguments!
顺便说说。要修复此错误,您不应该在try-except中包装函数调用。而是使用正确数量的参数调用它!
#3
You said
Now, I want to put the def statement in try. How to do this.
现在,我想把def语句放在try中。这该怎么做。
The def
statement is correct, it is not raising any exceptions. So putting it in a try
won't do anything.
def语句是正确的,它不会引发任何异常。所以试一试不会做任何事情。
What raises the exception is the actual call to the function. So that should be put in the try
instead:
引发异常的是对函数的实际调用。所以应该把它放在try中:
try:
test()
except TypeError:
print "error"
#4
If you want to throw the error at call-time, which it sounds like you might want, you could try this aproach:
如果你想在通话时抛出错误,听起来你可能想要,你可以试试这个方法:
def test(*args):
if args:
raise
print 'hi'
This will shift the error from the calling location to the function. It accepts any number of parameters via the *args
list. Not that I know why you'd want to do that.
这会将错误从调用位置转移到函数。它通过* args列表接受任意数量的参数。不是我知道你为什么要那样做。
#5
A better way to handle a variable number of arguments in Python is as follows:
在Python中处理可变数量的参数的更好方法如下:
def foo(*args, **kwargs):
# args will hold the positional arguments
print args
# kwargs will hold the named arguments
print kwargs
# Now, all of these work
foo(1)
foo(1,2)
foo(1,2,third=3)
#6
This is valid:
这是有效的:
try:
def test():
print 'hi'
except:
print 'error'
test()
#1
try:
test()
except TypeError:
print "error"
#2
In [1]: def test():
...: print 'hi'
...:
In [2]: try:
...: test(1)
...: except:
...: print 'exception'
...:
exception
Here is the relevant section in the tutorial
这是教程中的相关部分
By the way. to fix this error, you should not wrap the function call in a try-except. Instead call it with the right number of arguments!
顺便说说。要修复此错误,您不应该在try-except中包装函数调用。而是使用正确数量的参数调用它!
#3
You said
Now, I want to put the def statement in try. How to do this.
现在,我想把def语句放在try中。这该怎么做。
The def
statement is correct, it is not raising any exceptions. So putting it in a try
won't do anything.
def语句是正确的,它不会引发任何异常。所以试一试不会做任何事情。
What raises the exception is the actual call to the function. So that should be put in the try
instead:
引发异常的是对函数的实际调用。所以应该把它放在try中:
try:
test()
except TypeError:
print "error"
#4
If you want to throw the error at call-time, which it sounds like you might want, you could try this aproach:
如果你想在通话时抛出错误,听起来你可能想要,你可以试试这个方法:
def test(*args):
if args:
raise
print 'hi'
This will shift the error from the calling location to the function. It accepts any number of parameters via the *args
list. Not that I know why you'd want to do that.
这会将错误从调用位置转移到函数。它通过* args列表接受任意数量的参数。不是我知道你为什么要那样做。
#5
A better way to handle a variable number of arguments in Python is as follows:
在Python中处理可变数量的参数的更好方法如下:
def foo(*args, **kwargs):
# args will hold the positional arguments
print args
# kwargs will hold the named arguments
print kwargs
# Now, all of these work
foo(1)
foo(1,2)
foo(1,2,third=3)
#6
This is valid:
这是有效的:
try:
def test():
print 'hi'
except:
print 'error'
test()