有几种方法可以让我认为程序可能有这种行为,但所有这些方法看起来
不太可能。很难想象这会发生,除非有人故意这样做,作为一个恶作剧,或者作为一个抽象的难题/挑战的解决方案,比如
“你能让程序做这个吗?”
.
bar
可能导致
foo
立即返回。如果
酒吧
福
会在打印前停止,但也会
main
因为它没有
try
/
except
封锁。有些事你可以做
sys.excepthook
设置
global exception handler
,这可以解释为什么
酒吧
福
如果没有看到堆栈跟踪,但是异常处理程序不会导致
主要的
继续
福
离开了。
好吧,这是我解决这个难题的方法
我希望
不是你的程序真正在做什么:
def bar():
# we will change which function `print` refers to
global print
# keep a reference to the built-in `print` function
old_print = print
# define a function which ignores its argument, then restores the old `print` function
def ignore_one_print(s):
global print
print = old_print
# change `print` to be the function we just defined
print = ignore_one_print
当与代码结合时,它输出:
Only this line will get printed