python __all__
时间: 2023-10-20 11:07:35 浏览: 70
python中模块的__all__详细使用.docx
The `__all__` variable is a special variable in Python that is used to specify which names should be exported when a module is imported using the `from module import *` syntax.
When a module is imported with the `*` syntax, Python looks for the `__all__` attribute in the module and imports only the names listed in it. This allows developers to control what names are exported from their module and avoid polluting the global namespace with unwanted names.
For example, if a module named `my_module` has the following `__all__` attribute:
```python
__all__ = ['foo', 'bar']
```
Then, when the module is imported with the `*` syntax:
```python
from my_module import *
```
Only the names `foo` and `bar` will be imported, while any other names defined in the module will be ignored.
阅读全文