在编写python模块时,有没有办法防止它被客户端代码导入两次?就像 c/c++ 头文件一样:
#ifndef XXX
#define XXX
...
#endif
非常感谢!
Python 模块不会被多次导入。仅运行两次 import 不会重新加载模块。如果你想重新加载它,你必须使用reload
陈述。这是一个演示
foo.py
是一个单线模块
print("I am being imported")
这是多次导入尝试的屏幕记录。
>>> import foo
Hello, I am being imported
>>> import foo # Will not print the statement
>>> reload(foo) # Will print it again
Hello, I am being imported
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)