• @[email protected]
      link
      fedilink
      151 year 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]
        link
        fedilink
        191 year ago

        Because exit might be a variable you use to determine if you should exit. exit() is a function that actually does the exiting.

        It’s the difference between pointing at a jogger and saying “run” and actually running after them.

        • @[email protected]
          link
          fedilink
          171 year ago

          If you have a variable called exit you’ve overwritten the function in that scope, and won’t be able to execute it.

          e.g.

          >>> exit=1
          >>> exit()
          Traceback (most recent call last):
            File "", line 1, in 
          TypeError: 'int' object is not callable
          >>>
          
      • @[email protected]
        link
        fedilink
        141 year ago

        Guessing at what the programmer wants instead of implementing consistent behaviour is what Javascript does. Do you want Python to become Javascript?

      • @[email protected]
        link
        fedilink
        7
        edit-2
        1 year 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.

    • @[email protected]
      link
      fedilink
      221 year ago

      Incidentally, for anyone who hasn’t typed ‘import antigravity’ into an interactive Python terminal…you should - as Dr Seuss says, “These things are fun, and fun is good.”

      • @[email protected]
        link
        fedilink
        8
        edit-2
        1 year ago

        I love how it contains exactly one function: from antigravity import geohash

        Hell, this is the entire antigravity library:

        import webbrowser
        import hashlib
        
        webbrowser.open("https://xkcd.com/353/")
        
        def geohash(latitude, longitude, datedow):
            '''Compute geohash() using the Munroe algorithm.
        
            >>> geohash(37.421542, -122.085589, b'2005-05-26-10458.68')
            37.857713 -122.544543
        
            '''
            # https://xkcd.com/426/
            h = hashlib.md5(datedow, usedforsecurity=False).hexdigest()
            p, q = [('%f' % float.fromhex('0.' + x)) for x in (h[:16], h[16:32])]
            print('%d%s %d%s' % (latitude, p[1:], longitude, q[1:]))
        

        He literally gets a 32-bit hash, uses the first half of it as the latitude decimal, and the second half of it as the longitude decimal,

    • @jcgA
      link
      31 year ago

      You now start flying away

      And so does everything else, including all the AIR