46 ms·
>>> exit Use exit() or Ctrl-Z plus Return to exit
by pansa2 5y ago
>>> exit
Use exit() or Ctrl-Z plus Return to exit
- klodolph 5y agoIn Python, it is expected that typing an identifier will not cause an action to happen... so this is consistent with that expectation.
- gertlex 5y agoIn Python3, that is. I'd love to find (never looked...) a python3 repl where `print`, `dir`, `help` all behave like python2's `print`, since they're debug/lookup tools. It's rather often I'll open a terminal and want to check one of those things, and... typing () characters just adds significant effort (for lack of better description).
- wcoenen 5y agoThis is different, there is no special case handling here for you typing "exit". Python functions are invoked with parenthesis, while typing a name without parenthesis retrieves the content of a variable. The Python CLI helpfully sets the "exit" variable to that string so that you don't get a confusing NameError when you make this mistake.
- yatac42 5y agoIf the `exit` variable were set to a string, `exit()` would be an error because you can't use `()` on strings. `exit` is a callable whose `__str__` method returns that message. Note that this isn't specific to the REPL. Running `print(exit)` in a Python script will print the same message.
- vbezhenar 5y agoIt is result of calling `exit.__str__()`. This function could have called exit() itself instead.
- Taywee 5y agoI'd say that would be much more surprising and unintuitive behavior just for the sake of slightly more convenient REPL use. I wouldn't want stringifying any function to automatically call it. What if you store the function somewhere and print it for debugging, and then have to figure out why your program keeps crashing when you try to just print a list of functions? Besides, you usually have a more convenient exit available with Ctrl-D anyway.
- joebob42 5y agoNot for every function, just for exit.
- randallsquared 5y agoThe surprise is just not worth it.
- richard_todd 5y agoYeah but I can also say it’s a surprise to see instructions on how to exit the REPL when printing a list of functions. Honestly I think there should be no special case, and the REPL should print the “how to exit” text upon startup. As is, the user still has to guess ‘exit’ rather than ‘quit’ ‘abort’ ‘stop’ ‘bye’ etcetera to get the help text. (edit: actually they have the text on 'quit' as well)
- johnchristopher 5y ago$ bc -l bc 1.07.1 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. exit 0 quit Gets me everytime.
- DangitBobby 5y agoNotably, the ipython shell does the right thing here. That reason alone warrants an install in most virtual environments in my mind.