- cross-posted to:
- programmer_humor@programming.dev
- cross-posted to:
- programmer_humor@programming.dev
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?

They do different things lol
The main functions define entry points and the python example executes the code only if that script was invoked directly
Agreed, when building the wheel in python we can also define entry points like a main function, and run it the same way the compiled C code is run.
These people are comparing the interpreted raw python code to a compiled C binary. My dudes, bash doesn’t have a main function either and no one is fucking complaining.
I don’t see a lot of people building web servers in Bash. I think part of the point is that Python is a scripting language that likes to cosplay as a GPL.
Do you know the difference between a script and a program?
A script is what you give the actors; a program is what you give the audience.
Nobody expects bash to be remotely sane!
It’s less of a main, and more of a “don’t do this if being imported.” You can just throw code without that block and it will run.
the default function is
Idk, I guess I should ask why python needs a default function? If I’m running it as a script with commandline invocation I just copy and paste the if main namespace thing from stack overflow and it works as I intended. It also works if I invoke via python my_script.py $args, so I don’t really see why I should philosophically care about how other languages that I’m not using do it.
Now do for loops!
I mean yeah with no context that looks weird A/F, but given a couple details it’s fairly self-evident why it be like it do.
- Python doesn’t need a main function. It’ll just start running any top level code. This is nice for beginners learning programming because there’s no scaffolding or anything to set up. It just do the thing.
- python uses double underscores (usually called “dunder”) to mark “special” things.
__name__is a global variable containing the name of the current module. There’s a couple more like__file__containing the filename of the current module. - If you run a file directly
__name__gets set to"__main__". If it got set to something more sensible like"main"you couldn’t really call a filemain.pywithout this breaking. Right now this only breaks for files called__main__.pybut luck would have it that calling a file__main__.pyalready has a special meaning which makes these uses not clash. - Sometimes you want code to only run if a module is run but not when it is imported. Checking if
__name__is set to__main__is the easiest way to do this.
Python for sure has a bunch of weirdness, but it all does mesh together into a rather nice programming language.







