this post was submitted on 28 May 2025
712 points (96.2% liked)

Programmer Humor

23554 readers
2012 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 

Also, do y'all call main() in the if block or do you just put the code you want to run in the if block?

top 50 comments
sorted by: hot top controversial new old
[–] [email protected] 3 points 10 hours ago* (last edited 10 hours ago) (1 children)

wait till you see

if __name__ = "__main__":

   main()
`
[–] [email protected] 2 points 8 hours ago* (last edited 8 hours ago) (1 children)

Luckily Python is one step ahead:

Python 3.13.3 (main, Apr 22 2025, 00:00:00) [GCC 15.0.1 20250418 (Red Hat 15.0.1-0)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> if __name__ = "__main__":
... 
...    main()
...    
    File "<python-input-0>", line 1
    if __name__ = "__main__":
        ^^^^^^^^^^^^^^^^^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

Also TIL that := is a thing in Python.

[–] [email protected] 1 points 7 hours ago* (last edited 7 hours ago)

yea I also couldnt get the formatting to work right, triple quotes kept turning things into accented letters, so I gave up.

and also := also known as the walrus operator is very fun and sometimes very convenient to use

[–] [email protected] 3 points 20 hours ago (1 children)

I've always found needing to manually add a class instance parameter (i.e. self) to every object method really weird. And the constructors being named __init__. Not having multiple dispatch is kinda annoying too. Needing to use decorators for class methods, static methods, and abstract classes is also annoying. Now that I think about it, Python kinda sucks (even though it's the language I use the most, lol).

[–] [email protected] 3 points 15 hours ago (1 children)

Nah self is quite important. The main part of a method is to access the state of the object. self is just the interface to it.

[–] [email protected] 4 points 11 hours ago

Guess I just prefer languages that do it this way:

class AClass {
  var aProp = 0

  fun aMethod() {
    aProp++
  }
}

Though I suppose confusion and bugs can happen when you do something like:

class AClass {
  var aProp = 0

  fun aMethod(aProp: Int) {
    // `this.aProp` is needed to access the property
  }
}
[–] [email protected] 5 points 1 day ago

The if block is where my arg parser goes

[–] [email protected] 17 points 1 day ago (4 children)

I would put my code in a def main(), so that the local names don't escape into the module scope:

if __name__ == '__main__':
    def main():
        print('/s')
    main()

(I didn't see this one yet here.)

load more comments (4 replies)
[–] [email protected] 38 points 1 day ago (2 children)

One thing I really dislike about Python is the double underscore thing, just really looks ugly to me and feels excessive. Just give me my flow control characters that aren't whitespace

load more comments (2 replies)
[–] [email protected] 33 points 1 day ago* (last edited 1 day ago) (1 children)

Still better than having to create a new class just to implement

public static void main(String[] args) {}

Relevant Fireship video: https://youtu.be/m4-HM_sCvtQ

[–] [email protected] 18 points 1 day ago (4 children)
[–] [email protected] 1 points 13 hours ago

Impossible.

[–] [email protected] 15 points 1 day ago

Only took 27 years to make the Java "Hello, world!" kinda sane.

load more comments (2 replies)
[–] embed_me 44 points 2 days ago (5 children)

Sometimes I have the misfortune of working with python code written by someone else and I wonder how a language like this became anything more than a scripting language

[–] [email protected] 36 points 2 days ago (9 children)

I feel that Python is a bit of a 'Microsoft Word' of languages. Your own scripts are obviously completely fine, using a sensible and pragmatic selection of the language features in a robust fashion, but everyone else's are absurd collections of hacks that fall to pieces at the first modification.

To an extent, 'other people's C++ / Bash scripts' have the same problem. I'm usually okay with 'other people's Java', which to me is one of the big selling points of the language - the slight wordiness and lack of 'really stupid shit' makes collaboration easier.

Now, a Python script that's more than about two pages long? That makes me question its utility. The 'duck typing' everywhere makes any code that you can't 'keep in your head' very difficult to reason about.

[–] [email protected] 2 points 19 hours ago (1 children)

other people's Java

I'm gonna have to disagree here, it's always a guessing game of how many layers of abstraction they've used to seemingly avoid writing any implementation code... Can't put the code related to "bicycles" in the Bicycle class, no, that obviously goes in WheeledDeviceServiceFactoryBeanImpl that's in the 'utils' package.

[–] [email protected] 1 points 18 hours ago

Enough of that crazy talk - plainly WheeledDeviceServiceFactoryBeanImpl is where the dependency injection annotations are placed. If you can decide what the code does without stepping through it with a debugger, and any backtrace doesn't have at least two hundred lines of Spring boot, then plainly it isn't enterprise enough.

Fair enough, though. You can write stupid overly-abstract shit in any language, but Java does encourage it.

load more comments (8 replies)
load more comments (4 replies)
[–] [email protected] 11 points 1 day ago
[–] [email protected] 22 points 1 day ago* (last edited 1 day ago) (8 children)

It really doesn't. It's a scripting language, functions are there but at it's core it runs a script. The issue is that it was so easy to start with that people started doing everything in it, even though it sucks for anything past complex scripts

It is the excel of databases.

[–] [email protected] 13 points 1 day ago (16 children)

What's the difference between a "scripting" language and a "real" one?

[–] [email protected] 3 points 1 day ago

A scripting language controls an existing binary. A non-scripting language is used to create a new binary.

[–] JackbyDev 2 points 1 day ago

Scripting languages are real. Generally people consider dynamic languages scripting languages but it's not that simple.

load more comments (14 replies)
load more comments (7 replies)
[–] [email protected] 6 points 1 day ago (1 children)

Does everyone call the function of the script main? I never use main(), just call the function what the program is supposed to do, this program calculates the IBNR? The function is called calculate_IBNR(), then at the end of the script if name = 'main': calculate_IBNR(test_params) to test de script, then is imported into a tkinter script to be converter to an exe with pyinstaller

[–] [email protected] 3 points 15 hours ago

All of mine are called do_thing() because after a few days of working on it, the scope creep always means the original name was wrong anyway.

[–] [email protected] 85 points 2 days ago (12 children)

What kind of psychopath would put the code in the if block.

[–] [email protected] 74 points 2 days ago* (last edited 2 days ago) (3 children)

Looks at all the Python scripts in my bin folder that I wrote.

[–] [email protected] 63 points 2 days ago* (last edited 2 days ago) (7 children)

Never heard of

def main():
    pass

if __name__ == '__main__':
    main()

?

[–] [email protected] 49 points 2 days ago (8 children)

I remember how weird this looked the first time I saw it and while I may now understand it, it still looks jank af

[–] [email protected] 8 points 1 day ago

Python: I'm so readable that I'm practically executable pseudo-code

Also Python: if __name__ == '__main__': . . .

load more comments (7 replies)
load more comments (6 replies)
load more comments (2 replies)
load more comments (11 replies)
[–] [email protected] 26 points 2 days ago (3 children)

Python people explaining fail to see the point: Yes we know dunders exist. We just want you to say: "Yeah, that is a bit hacky, isn't it?"

[–] [email protected] 20 points 2 days ago* (last edited 2 days ago) (7 children)

Tbh reserving "main" is just a hacky if not more so than checking __name__ if you actually understand language design.

[–] [email protected] 1 points 19 hours ago (1 children)

Both are indeed equally terrible, even if it is for different reasons.

The one true choice is of course letting the programmer choose the main function in compile/interpretation-time.

I.e. python main.py --start "main" would start by calling the main function in main.py

[–] [email protected] 1 points 17 hours ago

Most contemporary python tools like flask or uvicorn do exactly this and require an explicit entry point

[–] namingthingsiseasy 8 points 1 day ago

Reserving main is definitely more hacky. Try compiling multiple objects with main defined into a single binary - it won't go well. This can make a lot of testing libraries rather convoluted, since some want to write their own main while others want you to write it because require all kinds of macros or whatever.

On the other hand, if __name__ == "__main__" very gracefully supports having multiple entrypoints in a single module as well as derivative libraries.

load more comments (5 replies)
load more comments (2 replies)
load more comments
view more: next ›