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
Programmer Humor
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
- Keep content in english
- No advertisements
- Posts must be related to programming or programmer topics
Could someone explain this please? I'm still a noob.
All code needs to have an entry point.
For Python and some other languages, this is the start of the file.
For other languages, this is a special function name reserved for this purpose - generally, "main".
In the first kind of language, the thought process is basically: I have the flow of execution, starting at the top of the file. If I want to make a library, I should build the things I want to build, then get out of the way.
In the other kind of language, the thought process is basically: I am building a library. If I want to make an executable, I should create an entry point they the execution starts at.
The debate is honestly pretty dumb.
if debug.getinfo(1).what == "main" then
-- ...
end
Not that you'll ever use it. No, seriously.
Edit: actually, they are not quite equivalent. This code just checks whether we are outside any function, not necessarily in the main file (i.e. not in a module). I don't think there's an equivalent to Python's __name__
in stock Lua.
"pythonic"
Nothing prevents you from putting a call to “main()” in the global scope
The point of the name==main logic is that it checks if that is the file that was invoked (like running python filename.py
). If you just put a main() in the global scope it will be called either when the file is invoked or loaded (which can cause unintended consequences).