1

asyncio パッケージの__init__.pyファイル (Python 3.4.1) を調べたところ、最後の数行に出くわしました。

"""The asyncio package, tracking PEP 3156."""

import sys

# The selectors module is in the stdlib in Python 3.4 but not in 3.3.
# Do this first, so the other submodules can use "from . import selectors".
# Prefer asyncio/selectors.py over the stdlib one, as ours may be newer.
try:
    from . import selectors
except ImportError:
    import selectors  # Will also be exported.

if sys.platform == 'win32':
    # Similar thing for _overlapped.
    try:
        from . import _overlapped
    except ImportError:
        import _overlapped  # Will also be exported.

# This relies on each of the submodules having an __all__ variable.
from .events import *
from .futures import *
from .locks import *
from .protocols import *
from .queues import *
from .streams import *
from .subprocess import *
from .tasks import *
from .transports import *

if sys.platform == 'win32':  # pragma: no cover
    from .windows_events import *
else:
    from .unix_events import *  # pragma: no cover


__all__ = (events.__all__ +
           futures.__all__ +
           locks.__all__ +
           protocols.__all__ +
           queues.__all__ +
           streams.__all__ +
           subprocess.__all__ +
           tasks.__all__ +
           transports.__all__)

上記のステートメントを考慮して、それらの名前が実際には存在しないはずなのにevents、他のすべてのサブモジュール (およびそれぞれの変数) にアクセスできる可能性はありますか? 私が見る限り、標準ライブラリのディレクトリは含まれておらず、サブモジュール自体も変数を定義していません。__all__from … import *asyncio/sys.pathsubmodule.__all__

余談__init__.pyですが、ファイルを見て実際に知りたかったのは、すべてのサブモジュールの名前を__all__パッケージのリストに自動的に追加する方法でし__init__.pyた。標準ライブラリの他のパッケージ)。私の現在のアプローチは次のとおりです。おそらく、あなたの答えは、asyncioパッケージがどのようにしてよりpythonicに見えるトリックをうまくやってのけるかを明らかにするでしょう。

from submodule1 import * # does not import __all__
from submodule1 import __all__ as submodule1_all
from submodule2 import *
from submodule2 import __all__ as submodule2_all

__all__ = submodule1_all + submodule2_all
4

0 に答える 0