Why should __all__ only contain string objects?

2019-02-20 14:36发布

问题:

Today I came across the following pylint error:

invalid-all-object (E0604):

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?

回答1:

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

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.

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]



回答2:

If you expose something other than a string, Python will throw an exception. This is why pylint gives that error, because the code is incorrect.

File mymodule.py:

def func():
    pass
__all__ = [func]

Now run:

from mymodule import *

You will get a TypeError.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: attribute name must be string, not 'function'

The reason is that __all__ is used to name attributes on the module object. That's just how the mechanism works. If you wanted to modify Python's import mechanism so that you could just put objects there, I suppose you could, but it would only work with certain types of objects (functions and classes would work, but constants would not work, and you wouldn't be able to rename functions and classes).



标签: python pylint