726

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
[-] qx128@lemmy.world 11 points 1 week ago

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

[-] jacksilver@lemmy.world 16 points 1 week ago

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

[-] littlewonder@lemmy.world 2 points 6 days ago

Dumb person question: if it's good practice to do this so things don't go sideways, shouldn't it be a built-in feature/utility/function/whatever?

[-] jacksilver@lemmy.world 2 points 6 days ago* (last edited 6 days ago)

It is "built-in" as the name is part of python. However, Python runs top to bottom, rather than having a special entrypoint. So name is just a utility you can use in your design.

While it can be a good practice to define a main entrypoint, that's more of a design decision and not hard rule. Many applications would not benefit from it because there is only one way to actually call the application to begin with.

Edit: Also not a dumb question. All programming languages have unique elements to them due to how they were envisioned and or developed over time (Pythons 30 years old)

[-] littlewonder@lemmy.world 2 points 6 days ago

I really appreciate the explanation!

[-] Static_Rocket@lemmy.world 7 points 1 week ago

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

[-] bastion@feddit.nl 1 points 1 week ago

Due to the oneness of all things, I refuse to distinguish between library code and executable code. One and Zero are arbitrary limitations.

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

Programmer Humor

23627 readers
2128 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