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

Programmer Humor

23899 readers
1521 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 1 week ago (5 children)
[–] bastion 7 points 1 week ago (4 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 week ago (2 children)

Can you elaborate on this blood magic?

[–] bastion 5 points 1 week ago* (last edited 1 week 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. :-)

Maybe I should do a post in programmer humor about preventing arbitrary execution of your python code, and make it sound like it prevents security flaws. ..which, technically, it does - if your code isn't executing, and instead a useless infinite loop is executing, there's nothing to exploit.

[–] slacktoid@lemmy.ml 1 points 1 week ago

Lol OK I was wondering how would this run

And yes you should!!

load more comments (1 replies)
load more comments (1 replies)