5 ms·
Another benefit is re-usability. If you want your program to be callable as module or script from elsewhere without going through the tedious shell functions, "
by el_programmador 6y ago
Another benefit is re-usability. If you want your program to be callable as module or script from elsewhere without going through the tedious shell functions, "def main()" is the only way to go.
- qw 6y agoWould that be different than checking the __name__? if __name__ = "__main__": mainMethod()
- el_programmador 6y agoThe difference is that you've encapsulated your coding logic in mainMethod() instead of writing under that if condition itself. If latter was the case, you can't call that code from another module without resorting to os shell level features.
- Nginx487 6y agoThe code encapsulated into "__main__" assumed to be module-specific, this is the purpose of that pattern. If you want to make some code available for another module, you just refactor it into separate function.
- tgb 6y agoBut the downside is that I like to use ipython and %run the file and if it's not in a function, I can then inspect all the intermediate variables and not just the result. Every time I do put it in a function, I regret it for this reason. Of course I could just use a real debugger...