I'm working on a large project that imports a lot of stuff. I have modules that import modules with from ... import *, and I want to avoid polluting module level variables when doing that.
For example:
module A:
import foobar
def foo():
pass
bar = 10
module B:
from A import *
# here, foobar is present, but I don't want it to be.
Obviously, the solution is to use __all__. So now it looks like this:
module A:
import foobar
def foo():
pass
bar = 10
__all__ = ["foo", "bar"]
module B:
from A import *
# here, foobar is no longer present
But this is tedious and time consuming.
Is it possible to generate __all__ dynamically, at runtime, without explicitly typing out all of the functions/classes that I want to export in a list?