this post was submitted on 11 Dec 2023
1168 points (98.9% liked)

Programmer Humor

32558 readers
451 users here now

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

Rules:

founded 5 years ago
MODERATORS
 
all 33 comments
sorted by: hot top controversial new old
[–] [email protected] 66 points 11 months ago (1 children)

“exit”

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

[–] [email protected] 15 points 11 months ago (4 children)

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] 19 points 11 months ago (1 children)

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] 17 points 11 months ago (2 children)

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] 17 points 11 months ago

Reminds me of setting pi = 3 in my friends matlab subroutines in school.

[–] [email protected] 5 points 11 months ago

wow it does do that. cool

[–] [email protected] 14 points 11 months ago (1 children)

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

[–] [email protected] 5 points 11 months ago (2 children)

Just once I want '1' + '2' to equal '3'. Is that so much to ask?

[–] [email protected] 7 points 11 months ago (1 children)

You want to remove the string concatenation operator? Cause that'll do it

[–] [email protected] 9 points 11 months ago (1 children)

I think every language needs a please operator, which acts to enforce human expectation of a statement:

'1'  + '1'            ## evaluates to '11'
please '1' + '1' ## evaluates to '2'
[–] [email protected] 4 points 11 months ago

I kinda like that

[–] [email protected] 5 points 11 months ago

Yes. Yes it is.

[–] [email protected] 7 points 11 months ago* (last edited 11 months 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.

[–] eluvatar 2 points 11 months ago

Because python has strong opinions

[–] [email protected] 41 points 11 months ago* (last edited 11 months ago) (2 children)

import gravity; gravity = None

edit: of course there is an xkcd: https://xkcd.com/353/.

[–] [email protected] 22 points 11 months ago (2 children)

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] 8 points 11 months ago* (last edited 11 months 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,

[–] [email protected] 6 points 11 months ago
[–] [email protected] 3 points 11 months ago

You now start flying away

And so does everything else, including all the AIR

[–] [email protected] 17 points 11 months ago (1 children)

What's the name of the Island, Java?

[–] [email protected] 7 points 11 months ago

Snake island

[–] [email protected] 10 points 11 months ago

See it's funny because we name things after other things

[–] [email protected] 6 points 11 months ago (2 children)

The Python REPL is fucking good.

[–] [email protected] 1 points 11 months ago* (last edited 11 months ago)

I see you've never used a Lisp REPL before.

[–] [email protected] 0 points 11 months ago (2 children)
[–] GuybrushThreepwo0d 5 points 11 months ago (2 children)

That's giving me a 404, captain

[–] [email protected] 3 points 11 months ago (1 children)
[–] [email protected] 2 points 11 months ago

You can search it yourself. The PYPI package is ptpython.

[–] [email protected] 2 points 11 months ago (1 children)
[–] [email protected] 3 points 11 months ago

On Sync it works fine. Moreover, it should work on other clients too. You better open a ticket.

[–] [email protected] 5 points 11 months ago (1 children)
[–] [email protected] 2 points 11 months ago