๐ฉ Whatโs the question?
Youโve created a Python module (a
but you donโt want all of them to be available when someone imports the module using
For example:
Now, if someone writes:
๐ป All three functions will be imported โ but you want to hide
โ So whatโs the solution?
You define a list named
Now if someone uses:
Theyโll get only
๐ก In sall
Everything not listed stays out โ though itโs still accessible manually if someone knows the name.
If this was confusing or you want a real example with output, just ask, my friend ๐กโค๏ธ
#Python #PythonTips #CodeClean #ImportMagic
๐By: https://xn--r1a.website/DataScienceQ
Youโve created a Python module (a
.py file) with several functions, but you donโt want all of them to be available when someone imports the module using
from mymodule import *.For example:
# mymodule.py
def func1():
pass
def func2():
pass
def secret_func():
pass
Now, if someone writes:
from mymodule import *
๐ป All three functions will be imported โ but you want to hide
secret_func.โ So whatโs the solution?
You define a list named
__all__ that only contains the names of the functions you want to expose:__all__ = ['func1', 'func2']
Now if someone uses:
from mymodule import *
Theyโll get only
func1 and func2. The secret_func stays hidden ๐๐ก In sall
__all__ list controls what gets imported when someone uses import *. Everything not listed stays out โ though itโs still accessible manually if someone knows the name.
If this was confusing or you want a real example with output, just ask, my friend ๐กโค๏ธ
#Python #PythonTips #CodeClean #ImportMagic
๐By: https://xn--r1a.website/DataScienceQ
๐6โค1๐ฅฐ1