Can someone explain __all__ in Python?

前端 未结 11 2426
一个人的身影
一个人的身影 2020-11-21 13:36

I have been using Python more and more, and I keep seeing the variable __all__ set in different __init__.py files. Can someone explain what this d

11条回答
  •  日久生厌
    2020-11-21 13:47

    Explain __all__ in Python?

    I keep seeing the variable __all__ set in different __init__.py files.

    What does this do?

    What does __all__ do?

    It declares the semantically "public" names from a module. If there is a name in __all__, users are expected to use it, and they can have the expectation that it will not change.

    It also will have programmatic affects:

    import *

    __all__ in a module, e.g. module.py:

    __all__ = ['foo', 'Bar']
    

    means that when you import * from the module, only those names in the __all__ are imported:

    from module import *               # imports foo and Bar
    

    Documentation tools

    Documentation and code autocompletion tools may (in fact, should) also inspect the __all__ to determine what names to show as available from a module.

    __init__.py makes a directory a Python package

    From the docs:

    The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.

    In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable.

    So the __init__.py can declare the __all__ for a package.

    Managing an API:

    A package is typically made up of modules that may import one another, but that are necessarily tied together with an __init__.py file. That file is what makes the directory an actual Python package. For example, say you have the following files in a package:

    package
    ├── __init__.py
    ├── module_1.py
    └── module_2.py
    

    Let's create these files with Python so you can follow along - you could paste the following into a Python 3 shell:

    from pathlib import Path
    
    package = Path('package')
    package.mkdir()
    
    (package / '__init__.py').write_text("""
    from .module_1 import *
    from .module_2 import *
    """)
    
    package_module_1 = package / 'module_1.py'
    package_module_1.write_text("""
    __all__ = ['foo']
    imp_detail1 = imp_detail2 = imp_detail3 = None
    def foo(): pass
    """)
    
    package_module_2 = package / 'module_2.py'
    package_module_2.write_text("""
    __all__ = ['Bar']
    imp_detail1 = imp_detail2 = imp_detail3 = None
    class Bar: pass
    """)
    

    And now you have presented a complete api that someone else can use when they import your package, like so:

    import package
    package.foo()
    package.Bar()
    

    And the package won't have all the other implementation details you used when creating your modules cluttering up the package namespace.

    __all__ in __init__.py

    After more work, maybe you've decided that the modules are too big (like many thousands of lines?) and need to be split up. So you do the following:

    package
    ├── __init__.py
    ├── module_1
    │   ├── foo_implementation.py
    │   └── __init__.py
    └── module_2
        ├── Bar_implementation.py
        └── __init__.py
    

    First make the subpackage directories with the same names as the modules:

    subpackage_1 = package / 'module_1'
    subpackage_1.mkdir()
    subpackage_2 = package / 'module_2'
    subpackage_2.mkdir()
    

    Move the implementations:

    package_module_1.rename(subpackage_1 / 'foo_implementation.py')
    package_module_2.rename(subpackage_2 / 'Bar_implementation.py')
    

    create __init__.pys for the subpackages that declare the __all__ for each:

    (subpackage_1 / '__init__.py').write_text("""
    from .foo_implementation import *
    __all__ = ['foo']
    """)
    (subpackage_2 / '__init__.py').write_text("""
    from .Bar_implementation import *
    __all__ = ['Bar']
    """)
    

    And now you still have the api provisioned at the package level:

    >>> import package
    >>> package.foo()
    >>> package.Bar()
    
    

    And you can easily add things to your API that you can manage at the subpackage level instead of the subpackage's module level. If you want to add a new name to the API, you simply update the __init__.py, e.g. in module_2:

    from .Bar_implementation import *
    from .Baz_implementation import *
    __all__ = ['Bar', 'Baz']
    

    And if you're not ready to publish Baz in the top level API, in your top level __init__.py you could have:

    from .module_1 import *       # also constrained by __all__'s
    from .module_2 import *       # in the __init__.py's
    __all__ = ['foo', 'Bar']     # further constraining the names advertised
    

    and if your users are aware of the availability of Baz, they can use it:

    import package
    package.Baz()
    

    but if they don't know about it, other tools (like pydoc) won't inform them.

    You can later change that when Baz is ready for prime time:

    from .module_1 import *
    from .module_2 import *
    __all__ = ['foo', 'Bar', 'Baz']
    

    Prefixing _ versus __all__:

    By default, Python will export all names that do not start with an _. You certainly could rely on this mechanism. Some packages in the Python standard library, in fact, do rely on this, but to do so, they alias their imports, for example, in ctypes/__init__.py:

    import os as _os, sys as _sys
    

    Using the _ convention can be more elegant because it removes the redundancy of naming the names again. But it adds the redundancy for imports (if you have a lot of them) and it is easy to forget to do this consistently - and the last thing you want is to have to indefinitely support something you intended to only be an implementation detail, just because you forgot to prefix an _ when naming a function.

    I personally write an __all__ early in my development lifecycle for modules so that others who might use my code know what they should use and not use.

    Most packages in the standard library also use __all__.

    When avoiding __all__ makes sense

    It makes sense to stick to the _ prefix convention in lieu of __all__ when:

    • You're still in early development mode and have no users, and are constantly tweaking your API.
    • Maybe you do have users, but you have unittests that cover the API, and you're still actively adding to the API and tweaking in development.

    An export decorator

    The downside of using __all__ is that you have to write the names of functions and classes being exported twice - and the information is kept separate from the definitions. We could use a decorator to solve this problem.

    I got the idea for such an export decorator from David Beazley's talk on packaging. This implementation seems to work well in CPython's traditional importer. If you have a special import hook or system, I do not guarantee it, but if you adopt it, it is fairly trivial to back out - you'll just need to manually add the names back into the __all__

    So in, for example, a utility library, you would define the decorator:

    import sys
    
    def export(fn):
        mod = sys.modules[fn.__module__]
        if hasattr(mod, '__all__'):
            mod.__all__.append(fn.__name__)
        else:
            mod.__all__ = [fn.__name__]
        return fn
    

    and then, where you would define an __all__, you do this:

    $ cat > main.py
    from lib import export
    __all__ = [] # optional - we create a list if __all__ is not there.
    
    @export
    def foo(): pass
    
    @export
    def bar():
        'bar'
    
    def main():
        print('main')
    
    if __name__ == '__main__':
        main()
    

    And this works fine whether run as main or imported by another function.

    $ cat > run.py
    import main
    main.main()
    
    $ python run.py
    main
    

    And API provisioning with import * will work too:

    $ cat > run.py
    from main import *
    foo()
    bar()
    main() # expected to error here, not exported
    
    $ python run.py
    Traceback (most recent call last):
      File "run.py", line 4, in 
        main() # expected to error here, not exported
    NameError: name 'main' is not defined
    

提交回复
热议问题