结论
- python
__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
版权声明:本文为qq_36743482原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。