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?
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
I call main() in the if
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.
Can you elaborate on this blood magic?
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. :-)
Lol OK I was wondering how would this run
And yes you should!!
Call the function from the if block.
Now your tests can more easily call it.
I think at my last job we did argument parsing in the if block, and passed stuff into the main function.
Can someone explain to me how to compile a C library with “main” and a program with main? How does executing a program actually work? It has an executable flag, but what actually happens in the OS when it encounters a file with an executable file? How does it know to execute “main”? Is it possible to have a library that can be called and also executed like a program?
There are a lot of other helpful replies in this thread, so I won’t add much, but I did find this reference, which you could read if you have a lot of free time. But I particularly liked reading this summary:
- _start calls the libc __libc_start_main;
- __libc_start_main calls the executable __libc_csu_init (statically-linked part of the libc);
- __libc_csu_init calls the executable constructors (and other initialisatios);
- __libc_start_main calls the executable main();
- __libc_start_main calls the executable exit().
main.py
or did you not read the manual?