In this blog, we will learn everything about the python __all__ function. So let’s get started.
What is python __all__ function
__all__ is a list of strings that specifies the names of the public objects that should be exported when the module is imported using the from module import * statement.
Objects not included in __all__ will not be exported and will not be available for use in the importing module.
Let’s take the below example
# mymodule.py def fun1(): pass def fun2(): pass class fun3: pass __all__ = ['fun1', 'fun2']
In this example, the fun1 function and the fun2 class are considered part of the public interface of the mymodule module, while the fun3 function is considered private and should not be used by external code.
When importing the module using the from mymodule import * statement, only fun1 and fun2 will be imported in the current namespace
Advantages of python __all__ function
There are several advantages of using __all__ attribute in Python:
- By explicitly defining the public interface of a module, other developers can quickly understand which objects are intended for external use and which are for internal use only.
- Using __all__ helps to organize the code in a more structured way, making it easier to maintain and understand.
- When importing specific objects from a module rather than using wildcard imports, naming conflicts with objects in the global namespace can be reduced.
- By using __all__ in conjunction with good documentation, it becomes clear which objects are part of the public API and how they should be used.
- When making changes to a module, it is easier to ensure backward compatibility by only modifying the internal objects, not the public interface.
- By only importing the necessary objects and not using wildcard imports, the performance of the program can be improved as it will import only what is needed.
python __all__ best practices
There are a few best practices to keep in mind when using __all__ attribute in Python:
- Use __all__ to explicitly define the public interface of your module. This makes it clear to other developers which objects are intended for external use and which are for internal use only.
- Keep the __all__ list short and focused. Don’t include every object in your module in the list, just the ones that are part of the public API.
- Avoid using wildcard imports (from mymodule import *) in your code. Instead, import specific objects using the import statement. This makes it clear which objects are being used and can help prevent name conflicts.
- Use __all__ in conjunction with good documentation. Clearly document the intended use and behavior of each object in the public API.
- Be consistent in using all across all your modules. This will make it easier for other developers to understand how to use your code.
- Remember that __all__ only affects the wildcard imports (from mymodule import *) or import(‘mymodule’). If a user imports a specific object from your module, it will be imported even if it is not in all.
Example of python __all__ function
below is another example of how to use the
__all__ variable in a Python module:
# testmodule.py def my_public_function(): print("This is a public function.") def _my_private_function(): print("This is a private function.") my_var1 = "This is a public variable." _my_var2 = "This is a private variable." __all__ = ["my_public_function", "my_var1"]
In this example, my_public_function and my_var1 are included in all, so they will be available for use in other modules that import testmodule. The _my_private_function and _my_var2 are not included in all, so they will not be exported and will not be available for use in other modules.
# main.py from testmodule import * my_public_function() #This is a public function. print(my_var1) #This is a public variable. my_private_function() #NameError: name 'private_function' is not defined print(_my_var2) #NameError: name '_var2' is not defined