Bacancy Technology
Bacancy Technology represents the connected world, offering innovative and customer-centric information technology experiences, enabling Enterprises, Associates and the Society to Rise™.
12+
Countries where we have happy customers
1050+
Agile enabled employees
06
World wide offices
12+
Years of Experience
05
Agile Coaches
14
Certified Scrum Masters
1000+
Clients projects
1458
Happy customers
Artificial Intelligence
Machine Learning
Salesforce
Microsoft
SAP
July 24, 2023
You can treat __all__ is a special variable that can be used to control what names are imported In case of from package import *. By default, the * operator will import all names from the module that is a public variable. However, if the module defines a __all__ variable, then only the names listed in __all__ will be imported.
For example, Module called my_module with two names: foo and bar. The __all__ variable is set to a list containing the name foo.
def foo(): print("foo") def bar(): print("bar") __all__ = ["foo"]
If we import my_module using the * operator, only the name foo will be imported. We can verify this by printing the names of all the names that were imported:
import my_module as m print(m.__all__)
This will print the following output:
['foo']
As you can see, only the name foo was imported. The name bar was not imported because it was not listed in the __all__ variable.
The __all__ variable is a way to specify which names from a module should be imported when using the from module import * syntax. This can be helpful for preventing other modules from accidentally importing names that they should not have access to.
Few notes about __all__: