我正在写一个python脚本,但我刚刚遇到有关python 3.x中的pylint检查的问题:
class m(object):
def check_infile(self):
infile = None
if not isinstance(infile, file):
print("infile variable is not a file type.")
输出:
E: 56,38: Undefined variable 'file' (undefined-variable)
我尝试通过添加# pylint: disable=E0602, undefined-variable, E0603
来消除此问题,但没有任何帮助。有什么建议吗?
答案 0 :(得分:2)
在python3.x中,file
不是有效类型:
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> file
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'file' is not defined
所以pylint就在这里,你可能不想让它沉默。您可能想要检查对象是否具有类似文件的方法:
if not getattr(infile, 'read', None):
print('definitely not a file...')