gpt4 book ai didi

python - 如果我从 sys 导入 exit(),它将运行哪个导出?

转载 作者:行者123 更新时间:2023-11-28 17:37:54 24 4
gpt4 key购买 nike

在 python 中,如果我从 sys 导入退出模块,它将运行正常的 exit() 或 sys.exit()

from sys import exit
print "Bla bla bla"
exit()

最佳答案

from a import b 将名为 b 的变量分配给导入的对象,因此您掩盖了内置的 exit

您也可以打印出 exit 的字符串表示来查看。运行

print(exit, type(exit))
from sys import exit
print(exit, type(exit))

看:

(Use exit() or Ctrl-D (i.e. EOF) to exit, <class 'site.Quitter'>)
(<built-in function exit>, <type 'builtin_function_or_method'>)

当然,很少有理由掩盖内置的。为什么不

import sys
sys.exit()

代替?

关于python - 如果我从 sys 导入 exit(),它将运行哪个导出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28621112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com