为什么 __all__ 应该只包含字符串对象? [英] Why should __all__ only contain string objects?

查看:31
本文介绍了为什么 __all__ 应该只包含字符串对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我遇到了以下pylint错误:

Today I came across the following pylint error:

无效的所有对象(E0604):

invalid-all-object (E0604):

__all__ 中的对象 %r 无效,必须仅包含字符串当 __all__ 中出现无效(非字符串)对象时使用.

Invalid object %r in __all__, must contain only strings Used when an invalid (non-string) object occurs in __all__.

我很好奇为什么直接暴露对象被认为是不正确的?

And I'm quite curious to why is it considered incorrect to expose objects directly?

推荐答案

因为它应该是名称的列表,而不是值:

Because it's supposed to be a list of names, not values:

如果标识符列表被星号('*')替换,则模块中定义的所有公共名称都绑定在import作用域的本地命名空间中代码>语句发生.

If the list of identifiers is replaced by a star ('*'), all public names defined in the module are bound in the local namespace for the scope where the import statement occurs.

模块定义的公共名称是通过检查模块的命名空间中名为__all__的变量来确定的;如果定义,它必须是由该模块定义或导入的名称的字符串序列.__all__ 中给出的名称都被认为是公共的,并且必须存在.如果 __all__ 未定义,则公共名称集包括在模块命名空间中找到的所有不以下划线字符('_')开头的名称.__all__ 应该包含整个公共 API.它旨在避免意外导出不属于 API 的项目(例如在模块中导入和使用的库模块).[语言参考]

The public names defined by a module are determined by checking the module’s namespace for a variable named __all__; if defined, it must be a sequence of strings which are names defined or imported by that module. The names given in __all__ are all considered public and are required to exist. If __all__ is not defined, the set of public names includes all names found in the module’s namespace which do not begin with an underscore character ('_'). __all__ should contain the entire public API. It is intended to avoid accidentally exporting items that are not part of the API (such as library modules which were imported and used within the module). [Language Reference]

这篇关于为什么 __all__ 应该只包含字符串对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆