【问题标题】:How should i use atexit in python?我应该如何在 python 中使用 atexit?
【发布时间】:2020-08-31 17:28:03
【问题描述】:

我在下面使用此代码,但它只输出“TypeError:第一个参数必须是可调用的”

import atexit

def end_script():
    print("ahh yes it works")

atexit.register(end_script())

while True:
    print("It still doesn't work")

atexit.register(x) 不应该只在脚本关闭时调用 x 吗? 我得到的所有参考都令人困惑,所以我有点迷失在这里。

【问题讨论】:

    标签: python-3.x atexit


    【解决方案1】:

    调用end_script并将其返回值传递给register。不要调用它,只需执行:

    atexit.register(end_script)  # No call parens after end_script
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-25
      • 2019-09-29
      • 1970-01-01
      • 2016-11-04
      • 2011-09-18
      • 2020-08-18
      • 2014-10-15
      • 2020-07-21
      相关资源
      最近更新 更多