我总是听到这个:
DeprecationWarning: integer argument expected, got float
我怎样才能让这条信息消失?在Python中有避免警告的方法吗?
我总是听到这个:
DeprecationWarning: integer argument expected, got float
我怎样才能让这条信息消失?在Python中有避免警告的方法吗?
当前回答
将实参转换为int。这很简单
int(argument)
其他回答
如果你知道你在做什么,另一种方法是简单地找到警告你的文件(文件的路径显示在警告信息中),注释生成警告的行。
你应该修改你的代码,但以防万一,
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
对于python 3,只需编写下面的代码来忽略所有警告。
from warnings import filterwarnings
filterwarnings("ignore")
当您希望仅忽略函数中的警告时,可以执行以下操作。
import warnings
from functools import wraps
def ignore_warnings(f):
@wraps(f)
def inner(*args, **kwargs):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("ignore")
response = f(*args, **kwargs)
return response
return inner
@ignore_warnings
def foo(arg1, arg2):
...
write your code here without warnings
...
@ignore_warnings
def foo2(arg1, arg2, arg3):
...
write your code here without warnings
...
只需在想要忽略所有警告的函数上添加@ignore_warnings装饰器
有点粗糙,但它为我工作后,上述方法没有。
- myscrypt。py 2 > / dev /空