Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the python __all__ module level variable for? [duplicate]

I've seen it a lot in python/Lib source code but I don't know what it is for.

I thought it was used to limit accessible members of of a module. So only the elements at __all__ will show up when dir(module).

I did a little example and saw it was not working as I expected.

So... What's the python __all__ module level variable for?

like image 295
Juanjo Conti Avatar asked Feb 02 '10 20:02

Juanjo Conti


People also ask

What is __ all __ In Python module?

It's a list of public objects of that module, as interpreted by import * . It overrides the default of hiding everything that begins with an underscore.

What does __ main __ mean in Python?

In Python, the special name __main__ is used for two important constructs: the name of the top-level environment of the program, which can be checked using the __name__ == '__main__' expression; and. the __main__.py file in Python packages.

How do you define __ all __?

Python __all__ is a variable that can be set in the __init__.py file of a package. The __all__ variable is a list of strings that defines those symbols that are imported when a program does.

What is the __ name __ variable in Python?

The __name__ variable (two underscores before and after) is a special Python variable. It gets its value depending on how we execute the containing script. Sometimes you write a script with functions that might be useful in other scripts as well. In Python, you can import that script as a module in another script.


2 Answers

It has two purposes:

  1. Anybody who reads the source will know what the exposed public API is. It doesn't prevent them from poking around in private declarations, but does provide a good warning not to.

  2. When using from mod import *, only names listed in __all__ will be imported. This is not as important, in my opinion, because importing everything is a really bad idea.

like image 135
John Millikin Avatar answered Oct 20 '22 00:10

John Millikin


http://docs.python.org/tutorial/modules.html#importing-from-a-package

Now what happens when the user writes from sound.effects import *? Ideally, one would hope that this somehow goes out to the filesystem, finds which submodules are present in the package, and imports them all. This could take a long time and importing sub-modules might have unwanted side-effects that should only happen when the sub-module is explicitly imported.

The only solution is for the package author to provide an explicit index of the package. The import statement uses the following convention: if a package’s __init__.py code defines a list named __all__, it is taken to be the list of module names that should be imported when from package import * is encountered. It is up to the package author to keep this list up-to-date when a new version of the package is released. Package authors may also decide not to support it, if they don’t see a use for importing * from their package.

like image 27
Jochen Ritzel Avatar answered Oct 19 '22 22:10

Jochen Ritzel