Reputation: 57033
The built-in function quit()
behaves differently in Python-3.x and IPython 3. The following code prints Hello
if executed in IPython, but does not print anything in Python:
quit()
print("Hello")
What is the purpose of quit()
in IPython? What was the reason for changing its behavior?
Upvotes: 4
Views: 848
Reputation: 155363
It looks like IPython
's quit
/exit
"function" simplifies to just setting a flag to say "you should exit when this is next checked". It doesn't raise SystemExit
itself, so it's presumably dependent on an intermittent check that, if you queue both commands at once, isn't performed until the second command finishes.
You can check it yourself at the IPython prompt, first run quit??
to see that it's a callable class whose __call__
delegates to self._ip.ask_exit()
. Next, run quit._ip.ask_exit??
and you'll see that ask_exit
just sets a flag, self.exit_now = True
(and it's a plain attribute if you check it, not a property
with hidden code execution).
You're welcome to track down where IPython
is checking that; I'm guessing it's done after any given line or cell of IPython
completes.
Fundamentally, the difference is that quit
in IPython has never been the same as quit
in regular Python's interactive interpeter; quit
/exit
as a built-in is intended to be replaced for alternate interactive interpreters, and needn't behave exactly the same. If you want a consistent exit behavior, import sys
and run sys.exit()
, which is also the correct way to exit early inside a script, and is not intended as a hook for the interactive prompt.
Upvotes: 6