Reputation: 303527
I'm writing a Python module and in one of my files I have a fairly complicated expression for __all__
:
# foo.py
__all__ = [ ... ]
In the top-level module that I want users to use, I want to expose all of that plus a few others. Do I just explicitly reference __all__
?
# the_module.py
import foo
__all__ = foo.__all__ + [ ... ]
or is there some way to do it using:
from foo import *
__all__ = ???
Upvotes: 1
Views: 360
Reputation: 122115
Note that __all__
is used to restrict the names that will be imported from a given module. If there is nothing in the_module.py
that users shouldn't be able to access, then you don't need to define it at all.
from foo import *
will bring in everything defined in foo.__main__
, so users that import the_module
will be able to access all of those names and anything defined in the_module.py
directly.
Upvotes: 1
Reputation: 6070
You can just try it:
# foo.py
__all__ = [1,2,3]
And then
# bar.py
import foo
print foo.__all__
output:
>>> python bar.py
[1, 2, 3]
Or if you want to import just __all__
directly:
# bar.py
from foo import __all__
print __all__
output:
>>> python bar.py
[1, 2, 3]
Python is very friendly to just going for it.
If you want to use the from module import *
form:
# foo.py
__all__ = [1,2,3]
__other_thing__ = [4,5,6]
And then
# bar.py
from foo import *
print foo.__all__
print foo.__other_thing__
output:
>>> python bar.py
[1, 2, 3]
[4, 5, 6]
Upvotes: 1