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?
-
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 call main() in the if
-
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?
What kind of psychopath would put the code in the if block.
-
What kind of psychopath would put the code in the if block.
wrote on last edited by [email protected]Looks at all the Python scripts in my bin folder that I wrote.
-
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?
Could someone explain this please? I'm still a noob.
-
Could someone explain this please? I'm still a noob.
Python has a bunch of magic variables, like
__name__
. This one contains the name of the module you're currently in (usually based on the file name), so if your file is calledfoo.py
, it will have the valuefoo
.But that's only if your module is being imported by another module. If it's executed directly (e.g.
python foo.py
), it will instead have a__name__
of__main__
. This is often used to add a standalone CLI section to modules - e.g. the module usually only defines functions that can be imported, but when executed it runs an example of those functions. -
Python has a bunch of magic variables, like
__name__
. This one contains the name of the module you're currently in (usually based on the file name), so if your file is calledfoo.py
, it will have the valuefoo
.But that's only if your module is being imported by another module. If it's executed directly (e.g.
python foo.py
), it will instead have a__name__
of__main__
. This is often used to add a standalone CLI section to modules - e.g. the module usually only defines functions that can be imported, but when executed it runs an example of those functions.checks username
So it's you they're always talking about
-
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?
Call the function from the if block.
Now your tests can more easily call it.
I think at my last job we did argument parsing in the if block, and passed stuff into the main function.
-
Could someone explain this please? I'm still a noob.
wrote on last edited by [email protected]Basically, when you compile a program written in Rust or C/C++ (the first and second panels respectively), the compiler needs to know what's supposed to be executed first when the program is run directly (i.e. when you click on the executable), which in these languages, is denoted by a special function called
main()
. Executable files can also contain functions and data structures that can be called by other programs, and when they are, you wouldn't want to run an entire complex and resource intensive program if another program only needs to call a single function from it. In that case, the other program will call the function it wants but not main, so only that function executes and not the entire program.However, Python is a scripting language that's interpreted. So every Python source file is executable provided you have the Python runtime. Python also doesn't have native support for main functions in the same way Rust and C/C++ does, and it will execute every line of code as it reads the source file. This is why a single line Python file that just calls print is valid, it doesn't need to be wrapped in a main function to execute. However, what if your Python file is both meant to be executed directly and provides functions that other Python files can call? If you just put the main routine in the root of the file, it would be executed every time another program tries to import the file in order to call functions from it, since the import causes the file to be interpreted and executed in its entirety. You can still just have a main function in your file, but since Python doesn't natively support it, your main function won't do anything if you run the file directly because as far as Python is concerned, there is no executable code at the root of the file and you haven't called any functions.
The workaround is to have a single if statement at the root of the file that looks like this:
if __name__ == '__main__': main()
It checks a special variable called
__name__
. If the Python file is directly executed,__name__
will have the value of the string'__main__'
, which satisfies the if statement so main() is called. If another Python file imports it, the value of__name__
will be the name of that file, so main() isn't called. It's clunky and not that efficient, but, 1, it works, and 2, if you cared about efficiency, you wouldn't be writing it in Python. -
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?
"pythonic"
-
What kind of psychopath would put the code in the if block.
I definitely do for quick scripts, but I try to break this habit. The biggest advantage of
def main()
is that variables are local and not accessible to other functions defined in the same script, which can sometimes help catch bugs or typos. -
Looks at all the Python scripts in my bin folder that I wrote.
custom bin folders are a realm no God dares to tread
-
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?
Its called
runpy.run_script
-
What kind of psychopath would put the code in the if block.
wrote on last edited by [email protected]I work in an academic / research environment. Depending who wrote it, even seeing a
__name__ == "__main__"
is a bit of a rare thing... -
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?
The
if
block is still in the global scope, so writing the code in it is a great way to find yourself scratching your head with a weird bug 30 minutes later. -
Looks at all the Python scripts in my bin folder that I wrote.
wrote on last edited by [email protected]Never heard of
def main(): pass if __name__ == '__main__': main()
?
-
Never heard of
def main(): pass if __name__ == '__main__': main()
?
wrote on last edited by [email protected]Heard of it, was too lazy to do it that way.
To be fair I now do it that way, but not when I was learning Python.
-
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?
Nothing prevents you from putting a call to βmain()β in the global scope
-
custom bin folders are a realm no God dares to tread
wrote on last edited by [email protected]I can and I do
*trollface*
-
Never heard of
def main(): pass if __name__ == '__main__': main()
?
I remember how weird this looked the first time I saw it and while I may now understand it, it still looks jank af