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

Programmer Humor

23585 readers
2905 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?

(page 2) 50 comments
sorted by: hot top controversial new old
[–] [email protected] 6 points 2 days ago (1 children)

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

load more comments (1 replies)
[–] [email protected] 20 points 3 days ago (8 children)

Could someone explain this please? I'm still a noob.

[–] [email protected] 5 points 3 days ago* (last edited 3 days ago) (2 children)

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.

load more comments (2 replies)
load more comments (7 replies)
[–] [email protected] 14 points 3 days ago* (last edited 3 days ago)
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.

[–] [email protected] 12 points 3 days ago
[–] [email protected] 11 points 3 days ago (2 children)

Nothing prevents you from putting a call to “main()” in the global scope

[–] [email protected] 16 points 3 days ago (3 children)

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).

load more comments (3 replies)
[–] [email protected] 7 points 3 days ago (1 children)

Just cross your fingers nobody attempts to import it...

load more comments (1 replies)
load more comments
view more: ‹ prev next ›