Python needs an actual default function
-
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
-
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.thats why i name my modules main.py
-
checks username
So it's you they're always talking about
It is, it's the other Barry.
-
I work in an academic / research environment. Depending who wrote it, even seeing a
__name__ == "__main__"
is a bit of a rare thing...Do you also have nothing but love for those 50+ cell Jupyter notebooks that don't use a single function and have everything in the global scope?
-
Do you also have nothing but love for those 50+ cell Jupyter notebooks that don't use a single function and have everything in the global scope?
the best thing is when not even the author knows the correct order of running the cells; because of course it isn't top-to-bottom.
-
I work in an academic / research environment. Depending who wrote it, even seeing a
__name__ == "__main__"
is a bit of a rare thing...Academic code is absolutely horrific.
Fortunately, it is possible to translate it for practical applications.
-
the best thing is when not even the author knows the correct order of running the cells; because of course it isn't top-to-bottom.
Yeah, and also zero dependency management, so you are free to figure out what combination of Python, Tensorflow and Keras will make it not throw random exceptions.
And don't forget the number one rule: you must use all the graphing libraries, all the time.
-
What kind of psychopath would put the code in the if block.
wrote on last edited by [email protected]If the file is just a class I usually put example usage with some default arguments in that block by itself. There is no reason for a "main" function. It's a nice obvious block that doesn't run when someone imports the class but if they're looking at the class there is a really obvious place to see the class usage. No confusion about what "main()" is meant to do.
if __name__ == '__main__': # MyClass example Usage my_object = MyClass() my_object.my_method()
-
Academic code is absolutely horrific.
Fortunately, it is possible to translate it for practical applications.
As someone in academia who writes code, I can confirm.
-
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?
wrote on last edited by [email protected]Alternative: put entry point code in file
__main__.py
& run the containing package (eg,some_package
) as a top-level expression (eg,python -m some_package
). -
What kind of psychopath would put the code in the if block.
Why would you waste a function call on something so completely redundant?
~For real though, arg parsing goes in the if, then gets dispatched to whatever function call is needed to run the proper script.~
-
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?
wrote on last edited by [email protected]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. -
Nothing prevents you from putting a call to βmain()β in the global scope
Just cross your fingers nobody attempts to import it...
-
Nothing prevents you from putting a call to βmain()β in the global scope
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). -
I remember how weird this looked the first time I saw it and while I may now understand it, it still looks jank af
Now think about this, you have logic that doesn't make sense when run directly, but you need it to be a library.
You have multiple name=main statements in some of your functions
-
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?
Sometimes I have the misfortune of working with python code written by someone else and I wonder how a language like this became anything more than a scripting language
-
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 people explaining fail to see the point: Yes we know dunders exist. We just want you to say: "Yeah, that is a bit hacky, isn't it?"