Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python __all__ variable

The __all__ variable is a special variable used in Python modules to define which names should be imported when a client imports a module using the wildcard *. By default, if the __all__ variable is not defined in a module, the import * statement imports all names that are defined in the module, except for those that begin with an underscore (_). However, it's considered a best practice to define the __all__ variable in your modules to explicitly specify which names should be imported.

In this tutorial, we will learn how to use the __all__ variable in Python modules.

  • Creating a Python module:

Let's create a Python module named my_module.py with the following content:

# my_module.py

def public_function():
    print("This is a public function.")

def _private_function():
    print("This is a private function.")

class PublicClass:
    pass

class _PrivateClass:
    pass

CONSTANT = 42

This module contains two functions, two classes, and a constant. By convention, names that begin with an underscore (_) are considered private and should not be used by external clients.

  • Importing names from the module without __all__:

If the __all__ variable is not defined in the module, the import * statement imports all public names (i.e., those that do not begin with an underscore).

# main.py

from my_module import *

public_function()  # This works
_PublicClass()     # This will raise an error, as _PrivateClass is not imported
  • Defining the __all__ variable in the module:

To explicitly specify which names should be imported when using the import * statement, you can define the __all__ variable in your module. The __all__ variable should be a list of strings that represent the names of the objects you want to make public.

Update the my_module.py module to include the __all__ variable:

# my_module.py

__all__ = ["public_function", "PublicClass", "CONSTANT"]

def public_function():
    print("This is a public function.")

def _private_function():
    print("This is a private function.")

class PublicClass:
    pass

class _PrivateClass:
    pass

CONSTANT = 42

Now, when a client imports the module using the import * statement, only the names listed in the __all__ variable will be imported.

# main.py

from my_module import *

public_function()  # This works
CONSTANT           # This works
_PublicClass()     # This will raise an error, as _PrivateClass is not imported

In summary, the __all__ variable in Python is used to explicitly define which names should be imported when a client imports a module using the wildcard *. It's a good practice to define the __all__ variable in your modules to provide a clear API for clients and prevent the accidental import of private names.