我正在尝试按照简单的代码运行
import sys
print("Starting Test Python Module");
def testmethod():
print("From test method")
sys.exitfunc = testmethod
print("Terminating Test Python Module");
它打印
C:\Users\athakur\Softwares>python test.py
Starting Test Python Module
Terminating Test Python Module
我无法理解为什么它不打印“来自测试方法”
虽然使用 atexit 效果很好
import atexit
print("Starting Test Python Module");
def testmethod():
print("From test method")
atexit.register(testmethod)
print("Terminating Test Python Module");
输出
C:\Users\athakur\Softwares>python test.py
Starting Test Python Module
Terminating Test Python Module
From test method