Python needs an actual default function
Python needs an actual default function
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?
Python needs an actual default function
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?
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.
__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.__name__
gets set to "__main__"
. If it got set to something more sensible like "main"
you couldn't really call a file main.py
without this breaking. Right now this only breaks for files called __main__.py
but luck would have it that calling a file __main__.py
already has a special meaning which makes these uses not clash.__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.
if __name__ == '__main__': exec(open(__file__, 'r').read())
I recall that Gondor very much needed a king to return, like it was a pretty big deal that returning as king to Gondor was a necessary deal. Gondor needed Aragorn to return as king. It's all documented in the third book, The Return of the King.
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.
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.
a> bash doesn’t have a main function either and no one is fucking complaining.
I don't complain about Bash's lack of features because I choose not to write Bash scripts and instead use saner languages.
Nobody expects bash to be remotely sane!
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.
It makes more sense if you're used to shell scripts.
It's a common practice but not required. Python behaves like JS where it just runs whatever you wrote. If you don't want it to run when importing the file you can put the main() inside the if so it only runs when you run the actual file.
You can use it when developing a function or a class to run a simple test without running the whole program.
python
#!/usr/bin/env python def main() -> None: """executes when file is run as a standalone script""" print('hello world') if __name__ == '__main__': main()
python
print('hello world')
If it's a quick dirty script like doing a one time update to multiple records then I'll just write it under the if name == main.
If it's a one time script you don't even need the if...
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.
Because if another consumer imports it you don't want it running the main method
I can import my_script2.py into my_script.py it doesn't run the main method unless I specifically invoke my_script2.main() though.
the default function is
There isn’t a default function. Module-level code is outside of any function^1.
I always put all of the code in the main block. Only exception is when I am creating a multithreaded/multiprocessor application. Then I normally use the if statement as the place to setup “the plumbing” with pipes and what not. That way people are forced to realize there is no main function but two co functions working in tandem
Depends on the situation but since Py3 you now have main.py in Python Packages
that is so cursed
And I say that as someone who learnt python as her first language (to be proficient in):
holy shit is this language stupid
I'm gonna repeat what I wrote in another comment. You are comparing the default interpreted way of running pythong to a compiled language. When you compile a python program into a wheel, you define the entry points of the code on it, and they point to functions like main() or whatever you define.
You aren't complaining that a index.js doesn't automatically run the main function when run by node, or that bash doesn't also have a default main function execution. Interpreted languages oriented to scripting won't have the same rules as compiled languages.
programming languages aren't usually cursed, what's cursed is the way some people decide to structure their programs. I've seen some stuff.
Now do for loops!