I have a helpers.py
file which defines about 30 helper functions to be exported as follows:
from helpers import *
To be able to do this, I have added all 30 functions to the __all__
variable. Can I automatically have all functions exported, rather than having to specify each one?
Actually I think Gandaro is right, you don't have to specify __all__
, but if, for some unknown reason, you would have to do it then, you can filter keywords from dir():
__all__ = [ helper for helper in dir() if helper == MY_CONDITION ]
Yes, by simply not specifying __all__
.
If you don't define __all__
then all of the functions in your module will be imported by calling from helpers import *
If you've got some functions that you'd like to keep private, then you could prefix their names with an underscore. From my testing, this stops the functions from being imported by import *
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With