2017-01-25 4 views

答えて

5

、ない値のリストことになっているので:

If the list of identifiers is replaced by a star ('*'), all public names defined in the module are bound in the local namespace for the scope where the import statement occurs.

The public names defined by a module are determined by checking the module’s namespace for a variable named __all__ ; if defined, it must be a sequence of strings which are names defined or imported by that module. The names given in __all__ are all considered public and are required to exist. If __all__ is not defined, the set of public names includes all names found in the module’s namespace which do not begin with an underscore character ('_'). __all__ should contain the entire public API. It is intended to avoid accidentally exporting items that are not part of the API (such as library modules which were imported and used within the module). [ Language Reference ]

3

文字列以外を公開すると、Pythonは例外をスローします。これはコードが正しくないため、pylintがそのエラーを出す理由です。

ファイルmymodule.py:

def func(): 
    pass 
__all__ = [func] 

今すぐ実行します。

from mymodule import * 

あなたはTypeErrorを取得します。

 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: attribute name must be string, not 'function' 

その理由は、__all__がモジュールオブジェクトの属性に名前を付けるために使用されているためです。それがメカニズムの仕組みです。 Pythonのインポートメカニズムを修正して、オブジェクトをそこに置くことができるようにしたいのであれば、あなたはできると思いますが、特定のタイプのオブジェクト(関数とクラスは動作しますが、定数は機能しません。関数とクラスの名前を変更できるようにする)。

関連する問題