Automatic export of all functions (vs manually with __all__)

I have a file helpers.pythat defines about 30 helper functions for export as follows:

from helpers import *

To do this, I added all 30 functions to the variable __all__. Can I automatically export all functions, and not specify them?

+5
source share
3 answers

Yes, just not pointing __all__.

+13
source

Actually, I think Gandaro is right, you do not need to specify __all__, but if for some unknown reason you have to do this, you can filter the keywords from dir ():

__all__ = [ helper for helper in dir() if helper == MY_CONDITION ]
+7
source

__all__, from helpers import *

If you have some features that you want to keep confidential, you can prefix their names with underscores. From my testing, this stops the execution of functions fromimport *

For example, in helper.py:

def _HiddenFunc():
    return "Something"

def AnActualFunc():
    return "Hello"

Then:

>>> from helper import *
>>> AnActualFunc()
'Hello'
>>> _HiddenFunc()
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
NameError: name '_HiddenFunc' is not defined
+6
source

All Articles