sys.exitfunc 在 python 中不工作

sys.exitfunc not working in python

我正在尝试运行遵循简单的代码

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

我无法理解为什么它不打印 "From Test method"

虽然使用 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

sys.exitfunc does not exist 在 Python 3.x.

sys.exitfunc is deprecated since python2.4 and was removed in python3.