__all__
用于限制from model import *
的模块导入;from model import B
指定具体模块的方式进行导入。test.py
如下:
# test.py
__all__ = ['A']
class A:
def __init__(self):
self.name = 'a'
class B:
def __init__(self):
self.name = 'b'
test_1.py
如下:
from test import *
def test():
a = A()
b = B()
print(a.name, b.name)
test()
# 运行报错 NameError: name 'B' is not defined
test_2.py
如下:
from test import A, B
def test():
a = A()
b = B()
print(a.name, b.name)
test()
# 正常运行,输出 a b