this post was submitted on 28 May 2025
735 points (96.3% liked)

Programmer Humor

32061 readers
414 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 3 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
[–] bastion@feddit.nl 7 points 1 year 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 1 year ago (1 children)

Can you elaborate on this blood magic?

[–] bastion@feddit.nl 5 points 1 year ago* (last edited 1 year 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 1 year ago

Lol OK I was wondering how would this run

And yes you should!!