上图警告的官网链接地址
上图警告信息是一个警告信息,提醒你在代码中使用了即将被弃用的函数或配置项,建议及时修改以避免在将来的版本中出现不兼容的情况。具体解释如下:
- 这段段警告信息来自于
pyreadline
模块的py3k_compat.py
文件,提示你使用了collections
模块中即将被弃用的函数collections.Callable
,建议使用collections.abc.Callable
代替。
我们发现以上警告信息问题是可以改变的,所以我们可以将collections.Callable
替换为collections.abc.Callable
。这是因为Python 3.3之后,collections
模块中的ABCs(Abstract Base Classes)在Python3.3中被移动到了collections.abc
模块中,并且在Python 3.10中使用collections.Callable
将不再支持。
因此,你应该使用以下代码来代替原来的代码:
from collections.abc import Callable
return isinstance(x, Callable)
这样可以避免上述警告信息,并且保证代码在将来的Python版本中仍然可用。
以上代码我们可以对其进行优化封装一下,以便在其他地方使用:
from collections.abc import Callable
def is_callable(x):
"""
检查x是否可调用
"""
return isinstance(x, Callable)
这样,在其他地方就可以直接调用is_callable
函数来检查一个对象是否可调用了,而不必每次都写一遍from collections.abc import Callable
和isinsta