4 ms·
These are all about passive experiences (which are great don't get me wrong!), but I think you can do better. It's the same phenomenon DHH talked about in the R
by dvrp 2y ago
These are all about passive experiences (which are great don't get me wrong!), but I think you can do better. It's the same phenomenon DHH talked about in the Rails doctrine when he said to "Optimize for programmer happiness".
The python excerpt is my favorite example:
```
$ irb
irb(main):001:0> exit
$ irb
irb(main):001:0> quit
$ python
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
```
<quote>
Ruby accepts both exit and quit to accommodate the programmer’s obvious desire to quit its interactive console. Python, on the other hand, pedantically instructs the programmer how to properly do what’s requested, even though it obviously knows what is meant (since it’s displaying the error message). That’s a pretty clear-cut, albeit small, example of [Principle of Least Surprise].
</quote>
- mixmastamyk 2y agoBad timing, this is changed in Python 3.12. Still I’ve always used Ctrl+D, which works everywhere unixy.
- AlienRobot 2y agoYes. I'd be surprised if exit without parentheses quit the interactive shell when it doesn't quit a normal python script.
- wodenokoto 2y agoIpython quits without parenthesis.
- nerdponx 2y agoIPython includes a whole lot of extra magic of various kinds, compared to the built-in Python console.
- pmarreck 2y agothis actually completely turned me off from python when I first encountered it. I was like... "the program KNEW WHAT I WAS TRYING TO DO, and instead of just DOING that it ADMONISHED me, fuck Python" LOL The proliferation of Python has only made my feelings worse. Try running a 6 month old Python project that you haven't touched and see if it still runs. /eyeroll
- tpmoney 2y ago>Try running a 6 month old Python project that you haven't touched and see if it still runs. My experience has been 6 month of python works fine. In fact, python is my go to these days for anything longer than a 5 line shell script (mostly because argparse is builtin now). On the other hand, running a newly written python script with a 6 month old version of python, that's likely to get you into trouble.
- fragmede 2y agoargparse? docopt or google-python-fire
- tpmoney 2y agoargparse, because argparse is built in. I'm usually writing shell scripts to automate some process for myself and my co-workers. The last thing I want is for them to have to be fiddling with installing external requirements if I can avoid it.
- tpmoney 2y agoOn the one hand, being generous in your inputs is always appreciated. On the other hand, the fact that both exit and quit will terminate ruby means the answer to "how do I quit ruby" now has two answers (technically 4 because `quit()` and `exit()` also work, and if we're talking about "least surprise" if you accept "exit" and "quit", why not also "bye" or "leave" or "close" or "end" or "terminate". Python might be surprising, but in this example, it's only surprising once, and helpful when it surprises you. Now you know quitting requires calling a function and that function is named exit() (although amusingly python3 anyway also accepts quit()). And being fully pedantic it doesn't know what you mean, it is assuming what you mean and making a suggestion, but that's not the same as knowing. From here on I'm not arguing the point anymore, just recording some of the interesting things I discovered exploring this in response to your comment: You can do this in python (which IMO is surprising, but in a different way): ``` >>> quit Use quit() or Ctrl-D (i.e. EOF) to exit >>> quit=True >>> quit True >>> quit() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'bool' object is not callable >>> exit() ``` But this also gives some sense to python's behavior. `quit` and `exit` are symbol names, and they have default assignments, but they're re-assignable like any other symbol in python. So the behavior it exhibits makes sense if we assume that they're not special objects beyond just being built int. `exit` is a class isntance according to type. So we should be able to create something similar, and indeed we can: ``` >>> class Bar: ... def __repr__(self): ... return "Type bar() to quit!" ... def __call__(self): ... print("I quit!") ... >>> bar = Bar() >>> bar Type bar() to quit! >>> bar() I quit! >>> ``` Interestingly this suggests we should be able to replace exit with our own implementation that does what ruby does if we really wanted too: ``` >>> class SuperExit: ... def __init__(self, real): ... self.real_exit=real ... def __repr__(self): ... print("Exiting via repr") ... self.real_exit() ... def __call__(self): ... print("Exiting via call") ... self.real_exit() ... >>> exit = SuperExit(exit) >>> exit Exiting via repr ```
- laserlight 2y ago> why not also "bye" or "leave" or "close" or "end" or "terminate". We can include these as well, but each keyword that you include brings diminishing returns at the cost of clutter and inconsistence in the API. Python problematically decides that returns diminish after the first --- “first” according to developers, that is --- possibility in all cases. Ruby anticipates that everyone's first choice will be different and practically maximizes comfort of users.
- FragenAntworten 2y ago`exit` will work as expected in Python 3.13: https://docs.python.org/3.13/whatsnew/3.13.html#whatsnew313-better-interactive-interpreter https://docs.python.org/3.13/whatsnew/3.13.html#whatsnew313-...