# test.py
l = [1, 2, 3]
# main.py
import test as t1
print(id(t1))
import test as t2
print(id(t2))
# 1120389060792
# 1120389060792
可以看到,在test.py中创建的变量l,只在第一次import的时候被初始化(id相同),所以完全可以作为单例模式使用(test.py中也可以搭配其他函数)
# test.py
l = [1, 2, 3]
# main.py
import test as t1
print(id(t1))
import test as t2
print(id(t2))
# 1120389060792
# 1120389060792
可以看到,在test.py中创建的变量l,只在第一次import的时候被初始化(id相同),所以完全可以作为单例模式使用(test.py中也可以搭配其他函数)