>>> print
<built-in function print>
>>> exit
<built-in function exit>
Hint: Use exit() or Ctrl-D (i.e. EOF) to exit
>>>
This way, it does do the thing you literally asked for, but also does help a newbie out.… or a long-time dev that switches languages fairly often :)
The regular Python REPL doesn't distinguish between stdout and stderr (perhaps it should!), but you can embed it in things like Jupyter notebooks that do.
That makes sense from a unix tool perspective, but not from a python repl perspective. The repl's behavior is to print the result from the last executed statement. For the exit function, the __repr__ method was overwriten to print the message. That way when you type in "exit", the last value would be exit (the function), and the overwritten __repr__ method causes the help message to be printed. There's no way to have it print to both without adding in some repl specific hacks.
>>> exit
Use exit() or Ctrl-Z plus Return to exit
>>> exit.__repr__()
'Use exit() or Ctrl-Z plus Return to exit' def __repr__(self):
import warnings
warnings.warn("Use exit() or Ctrl-Z plus Return to exit")
return super().repr()I think I agree though: what you really want to special case is the situation where the user types precisely 'exit<cr>' at a REPL prompt, and that hack needs to exist further up the stack than in the implementation of __repr__.
>>> print('%r' % str)
<class 'str'>
>>> def whatisit(x):
... print('It is %r' % x)
...
>>> whatisit(min)
It is <built-in function min>
>>> whatisit(exit)
It is Use exit() or Ctrl-D (i.e. EOF) to exit
Excuse me?IMO if the REPL wanted a friendly feature like this, it should be a generic REPL feature, not a hack applied to the function exit.
>>> def get_function():
... return exit
...
>>> a = get_function()
>>> a
Use exit() or Ctrl-D (i.e. EOF) to exit exit.__class__.__repr__ = lambda _: exit()
quit = exit
Edit, you also need this in your bashrc: export PYTHONSTARTUP=~/.pythonrcI think telnet does something stupid along the same lines "I know you want to quit but please ask address me as Sir first".
Programming languages have rules that the programmer is expected to learn. Part of being a programmer is foregoing a certain amount of user friendliness in favor of an environment that is more powerful so that we can actually get things done. Programmers are paid to memorize and follow these rules so that the end user does not have to learn them.
It's pretty easy to think of a good solution with few enough downsides that overall the design is much better:
In the REPL only, if you type "exit" and press enter, it quits.
No changes to Python semantics required. All code still works. Much friendlier UX.
I wonder what dubious justification the Python Devs would come up with to avoid implementing that (and therefore admitting they've been wrong for 20 years). They'd probably try and claim it is more confusing to beginners or some nonsense like that.
Is it too hard to write exit() or press Ctrl+D after having incorrectly entered exit?
[0] https://github.com/Source-Python-Dev-Team/Source.Python/blob...
When you work with _precise_ systems, they sometime take _precise_ input to work. That's kinda just part of the job.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
const b = 1
[1, 2, 3].forEach(console.log)
(from your linked page) does the wrong thing, because both forms (with and without a semicolon) are syntactically valid. I would much rather just always use semicolons (or, like Python, never use them) than have to memorise the 5% of oddball cases where I need to add them manually to resolve ambiguity.We have AI now that can write the entire code for you. Surely it's not much to ask that a compiler, as it parses your code and likely already has high confidence you need a semicolon in a certain spot, can just correct it for you and move on. Maybe have a --fix-errors option to the compiler command line or something.
The code is here: https://github.com/python/cpython/blob/3.12/Lib/_sitebuiltin...
The object shouldn't be in scope anywhere other than the REPL, but that doesn't mean that something, somewhere, isn't stringifying everything because $REASONS and changing the behaviour won't cause an obscure "crash" somewhere unexpected and hard to debug.
We saw this play it with web browsers in the late 90s early ‘aughts where they’d do their best to render what they thought you meant if your html was wrong, and it was indeed chaos.
Strict systems with strict inputs makes for a better overall ecosystem.
I agree; Python is actually helping you out here, since just typing `exit` doesn't actually call the callable.
Also, Python being Python, while not recommended, there's nothing stopping the user from assigning to `exit` then printing it in the repl:
>>> exit = 42
>>> exit
42
What would the author expect Python to do in this instance?