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

Programmer Humor

23563 readers
1094 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?

you are viewing a single comment's thread
view the rest of the comments
[–] slacktoid@lemmy.ml 51 points 3 days ago (1 children)
[–] bastion@feddit.nl 7 points 3 days ago (2 children)

I always use

if "__main__" == main:
    __main__()

..and earlier in the code:

def __main__():
    while True:
        pass
main = "__main__"

This helps to prevent people from arbitrarily running my code as a library or executable when I don't went them to.

[–] slacktoid@lemmy.ml 4 points 3 days ago (1 children)

Can you elaborate on this blood magic?

[–] bastion@feddit.nl 5 points 3 days ago* (last edited 3 days ago) (1 children)

It simply swaps some things around to make things more confusing, then goes into an infinite loop (whether or not you import or execute it standalone). it's no different than just including in the global scope:

while True:
    pass

I was kinda lazy with the fuckery, tbh. I could have gotten much more confusing, but don't have too much time today. :-)

[–] slacktoid@lemmy.ml 1 points 3 days ago

Lol OK I was wondering how would this run

And yes you should!!