There are several ways I can think of that a program might have this behaviour, but all of them seem really unlikely. It is hard to imagine that this could happen unless someone did it intentionally as a prank, or as the solution to an abstract puzzle/challenge, like "can you make a program do this?".
There is certainly no sensible way that calling bar
could cause foo
to return immediately. If bar
raises an exception, foo
would stop before printing, but so would main
since it has no try
/except
block. There are some things you can do with sys.excepthook
to set a global exception handler, which could explain why bar
is able to stop foo
without you seeing a stack trace, but the exception handler would not cause main
to continue where foo
left off.
Well, here is my solution to the puzzle, which I hope is not what your program is actually doing:
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
When combined with your code, it outputs:
Only this line will get printed