0
1

More than 3 years have passed since last update.

pythonの __all__ について

Last updated at Posted at 2020-06-20

pythonを以前勉強してた時期に、__all__について触れていましたが、なんだか最近なんだろうって迷い始めたので、ねんのためメモしておきます。

__all__というのは__init__.pyファイルに書く内容です。それで、allの中に書く文字列は全部このpythonモジュールがimportされる際にimportされます。

それでfrom package import *だと、このpackageの中にある先ほどの文字列の関数がimportされるわけです。それ以外はimportされません。

ということでした。以上

ちなみにですが、from package import *は非常に嫌われる書き方なので、できるだけ使わないように気をつけて

以下は参考にさせていただいた海外コミュニティにて質問された際の答えです。一応概要は上記で書いていますので、気になる方は御覧ください。

__all__ is a variable that can be set in the __init__.py file of a package.

The __all__ variable is a list of strings which defines those symbols that are imported when a program does

from package import *
If the __all__ for this package was set as follows:

__all__ = ['echo', 'effect', 'reverb']

then

from package import *

would be equivalent to

from package import echo
from package import effect
from package import reverb

Note that using from <package> import * is considered bad style in production code, since you have no control over what >you are importing and what your import might shadow.

for more information see : 6. Modules - Python 3.8.2 documentation

0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1