3 ms·
Except exit. I knew Python wasn't for me in my first foray into it when I fired its REPL and then went to exit it with control-C or whatever and it literally p
by pmarreck 5y ago
Except exit.
I knew Python wasn't for me in my first foray into it when I fired its REPL and then went to exit it with control-C or whatever and it literally printed out the right way to do it but then didn't do it. Python was more interested in having me do things a certain way even when it knew what I intended to do, just to be a twit.
- animal_spirits 5y agoCtrl-c raises a KeyboardInterrupt error, which is useful for programs to catch. If you type >>> exit Use exit() or Ctrl-D (i.e. EOF) to exit You will get that error response. The goal of this is to have the REPL language the exact same as the scripting language. exit() is supposed to be called as a function to make the language more consistent, so just typing `exit` will do nothing
- usrbinbash 5y ago> which is useful for programs to catch. Useful would be, if the default handler for SIGINT would not raise an exception, but have a useful default like eg. terminating the program. Go handles SIGINT this way by default. If I want an exception, I can just tell the program: import signal signal.signal(signal.SIGINT, throwException()) The way it is now, the exception bubbles up to runtime, and if it isn't handled (eg. in the REPL) the program crashes, or worse, hangs if there are other threads of execution running: import threading import time def sleepN(): for i in range(20): time.sleep(1) threading.Thread(target=sleepN).start() time.sleep(20) Press c-C here, and the thread will still run, because the bubbled up Excp only kills the main thread. This is a real footgun in applications which rely on SIGINT being a termination signal, and have long running threads.
- jrockway 5y agoThe REPL prints the value of a variable that you type in. exit is a variable, and so the REPL prints its value. If you want to run it as a function, you can do that, and indeed its string value is a message telling you to do that. $ python3 Python 3.9.2 (default, Feb 28 2021, 17:03:44) [GCC 10.2.1 20210110] on linux Type "help", "copyright", "credits" or "license" for more information. >>> exit Use exit() or Ctrl-D (i.e. EOF) to exit >>> exit.eof 'Ctrl-D (i.e. EOF)' >>> exit.name 'exit' >>> exit = 42 >>> exit 42 >>> exit() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not callable >>> I would have special-cased exit, though.
- jstx1 5y agoipython has 'exit' without the parentheses.