Hacker News new | past | comments | ask | show | jobs | submit login

    >>> exit
    Use exit() or Ctrl-Z plus Return to exit



This 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.


If 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.


It is result of calling `exit.__str__()`. This function could have called exit() itself instead.


I'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.


Yeah 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)


Not for every function, just for exit.


The surprise is just not worth it.


In Python, it is expected that typing an identifier will not cause an action to happen... so this is consistent with that expectation.


In 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).


Maybe you want Ruby instead?

I always found Ruby's optional parenthesis to be annoying in stored code, but I gotta admit it's nice on the REPL.


Have you considered ipython? It has commands like that.


Notably, the ipython shell does the right thing here. That reason alone warrants an install in most virtual environments in my mind.


  $ 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: