How to use Python __all__ : Implementation with Steps

How to use Python __all__ _ Implementation with Steps

You must have seen many programmers have used ” * ” with the import statement. What does it mean? It means they are importing all the attributes and functionalities of the specific module. These are generally specified by the python __all__ attribute. In this tutorial, you will learn what is it and how to use it in your custom module.

What is Python __all__?

Let’s understand python __ all___ in detail. Suppose you have a list of variables or functions in your custom module. You want the user to use it publicly when they import this module using the ” *”. If you have not defined the __all__ attribute inside then all the functionality will be imported thus increasing the size of the program. Therefore python __all__ attributes accept the list of variables and functions that will be imported when you will use the import ” * “.  It accepts the name of the variables, objects, e.t.c as string data types.

Why __all__ is Important for the developer?

There are many variables and functions defined inside the module. If you define the __all__ attribute then it prevents the leaks of variables or functions globally. Thus it makes it very easy to manage what functions or variables to be imported. Due to it, you can carefully control the API of the module.

Steps to use or implement python __all__?

Let’s know all the steps that you will take for a better understanding of the __all__ attribute.

Step 1: Create a Sample Module

In the first step, we will define some variables and functions for a sample module. For this example, the name of the module is “”

Add the below lines of code to the

name = "Data Science Learner"
domain = ""

def sum(a,b):
  return a+b

def multiply(a,b):
  return a*b


Step 2: Define the __all__attribute

After defining the variables and the functions, add the __all__ attribute. The __all__ attribute is a list of strings. Let’s add all the variables or functions to the list that will automatically import with the module when it is used with ” *”.

Let’s add the variable name and sum function to the __all__ attribute.

__all__ = ["name","sum"]

Step 3: Test the module

After defining the __all__ attribute let’s test the variables and function.

from sample import *


Printing the value of the name variable
Printing the value of the name variable
from sample import *


Printing the sum of two values
Printing the sum of two values
from sample import *


Getting the nameerror for multiply function
Getting the nameerror for multiply function
from sample import *


Getting the nameerror for domain variable
Getting the nameerror for domain variable

You can see we are getting the NameError when the variable or functions are in the list of __all__ attributes.


The python __all__ is a reserved variable that allows you to tell the python interpreter which variable or functions should be imported while using the module. Thus it prevents accidentally leaking variables and/or functions into the global namespace. Also, it makes it very easy to understand which functions or variables should be used by the developer.

I hope you have now understood what is python __all__ and how to use it in your projects. If you have any queries on it then you can contact us for more support.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner