I am currently using Python3 in Jupyter Notebook and I just ran into a keyword exit
. What does this keyword do ?
with open("some_file.txt") as f:
for lines in f:
print(lines)
exit
Just import 'exit' from the code beneath into your jupyter notebook (IPython notebook) and calling 'exit()' should work. It will exit and letting you know that...
The Notebook labels the cell with its number. The code that will be executed has the label In[ followed by the cell number, meaning Input. If there is a variable on its own at the end, that will generate an Output cell, labeled with Out[ followed by the cell number.
In python, we have an in-built quit() function which is used to exit a python program. When it encounters the quit() function in the system, it terminates the execution of the program completely.
Click F11, to view the Jupyter Notebook in Full Screen Mode. Click F11 once more, to come out of Full Screen Mode.
The exit
lines in your loop do nothing. Why they do nothing is a bit more complicated than the usual reason exit
would do nothing in Python, though.
Normally, exit
on a line by its own wouldn't exit Python. At most, in interactive mode, it would print a message telling you how to quit Python (message implemented in _sitebuiltins.Quitter.__repr__
):
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
IPython does something different. Among the many extra systems IPython has for interactive convenience is a system to autocall instances of a certain type, IPython.core.autocall.IPyAutocall
. (This is similar to but distinct from the %autocall
magic.)
In IPython, exit
and quit
are set to instances of IPython.core.autocall.ExitAutocall
, a subclass of IPyAutocall
. IPython recognizes objects of this type, so when a line containing just exit
or quit
is executed, IPython actually exits.
In [1]: exit
[IPython dies here]
A Jupyter notebook's IPython kernel has exit
and quit
set to instances of the very closely related IPython.core.autocall.ZMQExitAutocall
, which has some extra functionality to support a keep_kernel
argument, but is otherwise the same.
This functionality only triggers when a line referring to the autocallable object is the entire content of the cell, though. Inside a loop, the autocall functionality doesn't trigger, so we're back to nothing happening.
In fact, even less happens than what would happen in normal interactive mode - in a normal, non-IPython interactive session, this loop would print the "Use exit()..." message on each iteration, due to differences in how IPython and the regular interactive mode handle expression auto-printing.
When exit
(sic, with no parentheses) is used in iPython in a loop or a branch of a conditional statement, it is doing nothing because it is simply a reference to an instance of IPython.core.autocall.ExitAutocall
:
for i in range(10):
exit
print(i)
# 9
if i==9:
exit
print(exit)
# <IPython.core.autocall.ExitAutocall object at 0x7f76ad78a4a8>
It does not restart the kernel:
print(i)
# 9
However, when used on the command line alone, it is treated as a kind of magic (though without a %
) and terminates the kernel.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With