What is __all__ in python module?
__all__ attribure of a module controls list of attributes are imported by "from module import *". If it is not specified, the import will imports all public names from the imported module.
__all__ must be a list of strings. Each string indicate a name that will be imported.
For example, in below example,importing 'from urllib3 import *' has imported get_host function in local name space.
$ python
Python 3.7.1 (default, Dec 14 2018, 19:28:38)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from urllib3 import *
>>>
>>> locals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'HTTPConnectionPool': <class 'urllib3.connectionpool.HTTPConnectionPool'>, 'HTTPSConnectionPool': <class 'urllib3.connectionpool.HTTPSConnectionPool'>, 'PoolManager': <class 'urllib3.poolmanager.PoolManager'>, 'ProxyManager': <class 'urllib3.poolmanager.ProxyManager'>, 'HTTPResponse': <class 'urllib3.response.HTTPResponse'>, 'Retry': <class 'urllib3.util.retry.Retry'>, 'Timeout': <class 'urllib3.util.timeout.Timeout'>, 'add_stderr_logger': <function add_stderr_logger at 0x7ff8a716b1e0>, 'connection_from_url': <function connection_from_url at 0x7ff8a7117510>, 'disable_warnings': <function disable_warnings at 0x7ff8a6512510>, 'encode_multipart_formdata': <function encode_multipart_formdata at 0x7ff8a6581d08>, 'get_host': <function get_host at 0x7ff8a6571598>, 'make_headers': <function make_headers at 0x7ff8a65d3d90>, 'proxy_from_url': <function proxy_from_url at 0x7ff8a6592bf8>}
>>>
>>> get_host('http://google.com')
('http', 'google.com', None)
>>>
__all__ must be a list of strings. Each string indicate a name that will be imported.
For example, in below example,importing 'from urllib3 import *' has imported get_host function in local name space.
$ python
Python 3.7.1 (default, Dec 14 2018, 19:28:38)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from urllib3 import *
>>>
>>> locals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'HTTPConnectionPool': <class 'urllib3.connectionpool.HTTPConnectionPool'>, 'HTTPSConnectionPool': <class 'urllib3.connectionpool.HTTPSConnectionPool'>, 'PoolManager': <class 'urllib3.poolmanager.PoolManager'>, 'ProxyManager': <class 'urllib3.poolmanager.ProxyManager'>, 'HTTPResponse': <class 'urllib3.response.HTTPResponse'>, 'Retry': <class 'urllib3.util.retry.Retry'>, 'Timeout': <class 'urllib3.util.timeout.Timeout'>, 'add_stderr_logger': <function add_stderr_logger at 0x7ff8a716b1e0>, 'connection_from_url': <function connection_from_url at 0x7ff8a7117510>, 'disable_warnings': <function disable_warnings at 0x7ff8a6512510>, 'encode_multipart_formdata': <function encode_multipart_formdata at 0x7ff8a6581d08>, 'get_host': <function get_host at 0x7ff8a6571598>, 'make_headers': <function make_headers at 0x7ff8a65d3d90>, 'proxy_from_url': <function proxy_from_url at 0x7ff8a6592bf8>}
>>>
>>> get_host('http://google.com')
('http', 'google.com', None)
>>>
Comments
Post a Comment