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?
Because it's supposed to be a list of names, not values:
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:
Now run:
You will get a
TypeError
.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).