Open In App

Python __all__

Last Updated : 14 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Have you ever used a Python file’s variables after importing (`from F import *`) it into another Python file? In this scenario, every variable from that file can be used in the second file where the import is made. In this article, we’ll look at one Python mechanism, called __all__, which allows users to import only some variables rather than the entire set of variables from a Python file.

What is __all__ in Python?

In Python, `__all__` is a list of strings that defines what names should be imported from the current module (Python file), to another file. Only the variables that are declared in that list can be used in that other file after importing this file; the other variables, if referenced, will throw an error.

It also works in a package: in that case `__all__` is defined in the package’s `__init__.py` file, and lists the public modules that will be imported from that package when `from package import *` is used.

Benefits of __all__ in Python

  • Security Purpose: While importing the data from one Python file to another Python file, all the variables of the first file are usable in the second file. But due to security purposes, we want the second file to use only certain variables and not all the variables of the first file, then we can use the __all__ variable in the first Python file and define the usable variables in it.
  • Usage of the same variable name: Let us suppose, we have declared one variable, titled XYZ in the first Python file and the first Python file is being called by another Python file, then all the variables of the first file will be imported into the second file. But in case, we have no use of the XYZ variable of the first file in the second file, then we can limit it by using __all__, and declare if needed the XYZ variable again in the second file and use it. Note that if you redefine a variable imported by `*`, your definition will have precedence over the imported one.

Syntax of __all__  in Python

__all__ = ['variable_1', 'variable_2']

Example:

First of all, in the file that will be imported via `from file import *`, declare some variables and assign them values. Then, create a list of the variables that you want to be imported into another file and declare them in the __all__ variable.

gfg1.py

Let us suppose we have two files gfg1.py and gfg2.py. We need to import certain variables of gfg1.py into gfg2.py. Thus, whatever we want to get imported from file1 to file2, we will define it in the __all__ variable, which is basically a list of strings.

Python3
# Declare some variables
app = 10
ball = True
cat = 'kitten'
dog = 100.0

# Define variables to be imported
# when module is imported
__all__ = ['app', 'ball']

Case 1: Calling the variable ‘app’ from the gfg1.py file

In this example, we will import the gfg1.py file in the gfg2.py file and print the value of the variable app declared in the gfg1.py file.

Code for file gfg2.py:

Python
# Importing the gfg1.py file
from gfg1 import *

# Print the variable app
print(app)

Output:

What is _all __ in Python?

Case 2: Calling the variable ‘ball’ from the gfg1.py file

In this example, we will import the gfg1.py file in the gfg2.py file and print the value of the variable ball declared in the gfg1.py file.

Code for file gfg2.py:

Python3
# Importing the gfg1.py file
from gfg1 import *

# Print the variable ball
print(ball)

Output:

What is _all __ in Python?

Case 3: Calling the variable ‘cat’ from the gfg1.py file

In this example, we will import the gfg1.py file in the gfg2.py file and print the value of the variable cat declared in the gfg1.py file. As the variable is not declared under the __all__ list, it should pop up the error.

Code for file gfg2.py:

Python3
# Importing the gfg1.py file
from gfg1 import *

# Print the variable cat
print(cat)

Output:

What is _all __ in Python?

Case 4: Calling the variable ‘dog’ from the gfg1.py file

In this example, we will import the gfg1.py file in the gfg2.py file and print the value of the variable dog declared in the gfg1.py file.  As the variable is not declared under the __all__ list, it should pop up the error.

Code for file gfg2.py

Python3
# Importing the gfg1.py file
from gfg1 import *

# Print the variable dog
print(dog)

Output:

What is _all __ in Python?

Note: As __all__ is a list, so we can only pass the string values in the list and not the function. Let us take an example of passing a function in __all__ and calling it to see what will happen in that case. We can see clearly that it popped out the error ‘Item in __all__ must be str, not function.’

Python3
# Program to pass function
# in __all__ in Python

# Declare some variables
a = 10
b = 6

# Define names to be imported
# when module is imported
__all__ = ['a', 'b', lambda a,b:a+b]

Output:

What is _all __ in Python?

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads