首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未定义- python 3.x在pylint中的变量警告

未定义- python 3.x在pylint中的变量警告
EN

Stack Overflow用户
提问于 2016-04-11 15:19:11
回答 1查看 2.1K关注 0票数 0

我正在编写python脚本,但我只是在python 3.x中遇到了有关pylint检查的问题:

代码语言:javascript
复制
class m(object):
    def check_infile(self):
        infile = None
        if not isinstance(infile, file):
            print("infile variable is not a file type.")

输出:

代码语言:javascript
复制
E: 56,38: Undefined variable 'file' (undefined-variable)

我试图通过添加# pylint: disable=E0602, undefined-variable, E0603来消除这个问题,但是没有任何帮助。有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-11 15:22:20

在python3.x中,file不是有效类型:

代码语言:javascript
复制
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

所以皮林特就在这里,你可能不想让它安静下来。您可能需要检查对象是否具有类似文件的方法:

代码语言:javascript
复制
if not getattr(infile, 'read', None):
    print('definitely not a file...')
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36552767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档