Python needs an actual default function
-
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?"
-
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
Sounds like a skill issue on your end
-
Never heard of
def main(): pass if __name__ == '__main__': main()
?
What is the point of this?
-
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]I use if__name__main__ often when working with AWS Lambda, but I also want to run it locally. Lambda wants to call a function with the params
event
andcontext
. So I would do something like this:def handler(event, context): things return { 'statusCode': 200, 'body': 'Hello from Lambda!' } if __name__ == '__main__': event = {} context = {} response = handler(event, context) print(response)
-
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
I feel that Python is a bit of a 'Microsoft Word' of languages. Your own scripts are obviously completely fine, using a sensible and pragmatic selection of the language features in a robust fashion, but everyone else's are absurd collections of hacks that fall to pieces at the first modification.
To an extent, 'other people's C++ / Bash scripts' have the same problem. I'm usually okay with 'other people's Java', which to me is one of the big selling points of the language - the slight wordiness and lack of 'really stupid shit' makes collaboration easier.
Now, a Python script that's more than about two pages long? That makes me question its utility. The 'duck typing' everywhere makes any code that you can't 'keep in your head' very difficult to reason about.
-
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?"
wrote on last edited by [email protected]Tbh reserving "main" is just a hacky if not more so than checking
__name__
if you actually understand language design. -
I feel that Python is a bit of a 'Microsoft Word' of languages. Your own scripts are obviously completely fine, using a sensible and pragmatic selection of the language features in a robust fashion, but everyone else's are absurd collections of hacks that fall to pieces at the first modification.
To an extent, 'other people's C++ / Bash scripts' have the same problem. I'm usually okay with 'other people's Java', which to me is one of the big selling points of the language - the slight wordiness and lack of 'really stupid shit' makes collaboration easier.
Now, a Python script that's more than about two pages long? That makes me question its utility. The 'duck typing' everywhere makes any code that you can't 'keep in your head' very difficult to reason about.
How do you feel about other peoples Go code?
-
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]Can someone explain to me how to compile a C library with "main" and a program with main? How does executing a program actually work? It has an executable flag, but what actually happens in the OS when it encounters a file with an executable file? How does it know to execute "main"? Is it possible to have a library that can be called and also executed like a program?
-
I use if__name__main__ often when working with AWS Lambda, but I also want to run it locally. Lambda wants to call a function with the params
event
andcontext
. So I would do something like this:def handler(event, context): things return { 'statusCode': 200, 'body': 'Hello from Lambda!' } if __name__ == '__main__': event = {} context = {} response = handler(event, context) print(response)
Diabolical
-
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?
Shouldn't the third panel just be empty?
-
Can someone explain to me how to compile a C library with "main" and a program with main? How does executing a program actually work? It has an executable flag, but what actually happens in the OS when it encounters a file with an executable file? How does it know to execute "main"? Is it possible to have a library that can be called and also executed like a program?
I haven't done much low level stuff, but I think the 'main' function is something the compiler uses to establish an entry point for the compiled binary. The name 'main' would not exist in the compiled binary at all, but the function itself would still exist. Executable formats aren't all the same, so they'll have different ways of determining where this entry point function is expected to be. You can 'run' a binary library file by invoking a function contained therein, which is how DLL files work.