1172
submitted 2 years ago by [email protected] to c/[email protected]
you are viewing a single comment's thread
view the rest of the comments
[-] [email protected] 66 points 2 years ago

“exit”

✈️: Use exit() or Ctrl-D (I.e. EOF) to exit

[-] [email protected] 15 points 2 years ago

I mean if they can see that we type exit and show us this message, why could they not just start the exiting when we type exit?

[-] [email protected] 7 points 2 years ago* (last edited 2 years ago)

This is the code (Github link):

class Quitter(object):
    def __init__(self, name, eof):
        self.name = name
        self.eof = eof
    def __repr__(self):
        return 'Use %s() or %s to exit' % (self.name, self.eof)
    def __call__(self, code=None):
        # Shells like IDLE catch the SystemExit, but listen when their
        # stdin wrapper is closed.
        try:
            sys.stdin.close()
        except:
            pass
        raise SystemExit(code)

What happens is that the python repl calls __repr__ automatically on each variable/statement that you type into the repl (except assignments e.g. x = 1). But this basically only happens in the repl. So "executing" only exit wouldn't work in a python script as it is not calling __repr__ automatically, so better you learn how to do it right than using just exit in your python scripts and scratching your head why it works in the repl but not in your code.

load more comments (11 replies)
load more comments (11 replies)
this post was submitted on 11 Dec 2023
1172 points (98.9% liked)

Programmer Humor

37126 readers
58 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS