GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

A python question

I am new to Python and have been using it more and more, and I keep seeing the variable __all__  set in different __init__.py files. Can someone explain what this does?

asked Nov 20, 2015 by Swanky

1 Answer

__all__ is a special variable that is used to include all the modules in a packages. When you write from import * statement, all the modules listed in __init__.py and all the values in __all__ gets imported. This is a shortform of importing all the modules of a package, othereise you will have to type "import module_name1",  "import module_name2", .... for each of the module. 

Any folder say p1 containing __init__.py makes it package.

for example, if a folder f1 has the modules m1.py, m2.py and m3,py

then, __init__.py can have __all__ = ['m1','m2','m3']. If you from p1 import * , then all the modules m1,m2,m3 can be imported. Also you can import them individually. 

NOTE : But if you write just import p1, and you only wrote __all__ = ['m1', 'm2'],  then no modules will be imported.

 

answered Nov 20, 2015 by harshitgarg
...