5 ms·
First example seems odd? The author acknowledges that the two proposed alternatives (printing "<function exit>" or actually exiting) aren't great. The fact that
by mrblampo 3y ago
First example seems odd? The author acknowledges that the two proposed alternatives (printing "<function exit>" or actually exiting) aren't great. The fact that the Python REPL provides a helpful hint seems like extra effort to make your life easier. This is especially notable given that in the next example, the author's complaint is about a command _not_ providing special guidance about the thing the user is probably trying to do.
- pluc 3y agoYeah. "I know it's not right, but it's not totally wrong and probably better than not doing anything" actions as maddening as the behaviour he describes. A world where that's the standard would be chaos. When you work with _precise_ systems, they sometime take _precise_ input to work. That's kinda just part of the job.
- phs318u 3y agoExcept in the case of the 'exit' clearly this is accepting and parsing imprecise input in order to guide the user. If the code wasn't looking for 'exit' in order to correct the user (to use 'exit()'), it would just spit out some other error for 'I don't know what you're talking about'. Instead, they actually detect the intent by specifically coding for it... and then ignore it anyway. That's bloody minded.
- gruez 3y agoHow do you feel about java/c++ compiler that give hints like "missing semicolon"? Clearly it knows what's wrong. Why not automagically fix that for you[1]? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#automatic_semicolon_insertion https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- akoboldfrying 3y agoJavaScript automatic semicolon insertion does a great job most of the time, but still leads to bafflement when code like 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.
- ryandrake 3y agoI'd actually love that feature! 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.
- andrewaylett 3y agoExcept they didn't -- the result of a statement is turned into a string, and the string is printed. There are standard ways of turning objects into strings, and the `__repr__` function on the `exit` object returns that string. If you call that object then it raises an exception that triggers a REPL to cleanly quit. The code is here: https://github.com/python/cpython/blob/3.12/Lib/_sitebuiltins.py#L18 https://github.com/python/cpython/blob/3.12/Lib/_sitebuiltin...
- saulpw 3y agoHow about making `exit.__repr__` raise a CleanExit exception that gets caught in the outer REPL and then exits?
- andrewaylett 3y agoI'm fairly sure that having any object's `__repr__` throw an exception and exit would lead to even more confusion. Especially if it exits cleanly. 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.
- edflsafoiewq 3y agoIf it actually exited, you might incorrectly conclude that exit would work the same way in a script.
- imron 3y ago> A world where that's the standard would be chaos. 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.
- dale_glass 3y agoI'd have done both the hint and the actual result. Like this: >>> 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.
- ssgodderidge 3y ago> does help a newbie out … or a long-time dev that switches languages fairly often :)
- dmurray 3y agoIt should print the first to stdout and the second to stderr. That's completely consistent with how stdout and stderr are usually used. 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.
- gruez 3y ago> It should print the first to stdout and the second to stderr. That's completely consistent with how stdout and stderr are usually used. 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'
- dmurray 3y agoHow about def __repr__(self): import warnings warnings.warn("Use exit() or Ctrl-Z plus Return to exit") return super().repr()
- andy99 3y agoVim iirc has a similar hint telling you to exit when you try something that should exit. I don't remember how it's triggered, ctrl-q maybe (edit, ctrl-c). I see the point, if you know what someone is trying to do, better to just do it, or ignore it, not give a condescending "nudge". Though personally the hint doesn't bother me.
- nneonneo 3y agoYeah, and it’s also useful to teach newbies about the usual syntax for doing things rather than introducing special-case magic. It would be _weird_ to just quit after writing a bare “exit”.
- gizmo 3y agoipython intentionally diverges from the regular python REPL. It just quits like you expect without crying about it.
- devjam 3y ago> The fact that the Python REPL provides a helpful hint seems like extra effort to make your life easier. 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?
- MichaelDickens 3y agoI think the current behavior is worse simply exiting. The only concern with exiting when the user types "exit" is that perhaps the user actually wanted to see the value of the `exit` function. But the current behavior doesn't show the value of the `exit` function either, so it's useless for that use case.