一个xx.py文件就是一个python模块 module
当一个xx.py中定义__all__属性的时候,会影响从模块导入公共变量。
一个包含__init__.py文件的文件夹称为包 package
- test
- a.py
- b.py
- __init__.py
a.py
__all__ = ["func1"]
def func1():
print("hello world")
def func2():
print("bye world")
使用from module import attr 方式__all__属性列表不起作用
b.py
from a import func1, func2
func1()
func2()
"""
hello world
bye world"
"""
使用from module import * 方式导入
b.py
from a import *
func1()
func2()
"""
hello world
NameError: name 'func2' is not defined. Did you mean: 'func1'?
"""