5 ms·
One benefit of using a main function in Python is performance. If you’re doing significant computation at the top-level, it’ll probably run faster if moved into
by pansa2 6y ago
One benefit of using a main function in Python is performance. If you’re doing significant computation at the top-level, it’ll probably run faster if moved into a function.
I believe this comes from the fact that local-variable lookup is faster than global-variable lookup - because the former is lookup-by-index and the latter is lookup-by-name.
The same is true in Julia: “Any code that is performance critical or being benchmarked should be inside a function.” [0]
[0] https://docs.julialang.org/en/v1/manual/performance-tips/index.html https://docs.julialang.org/en/v1/manual/performance-tips/ind...
- xtreak29 6y agoThere were some optimisations done in https://bugs.python.org/issue26219 https://bugs.python.org/issue26219
- Cactus2018 6y agoWhat a great patch history. Love this: >> Would it be OK if we add this to 3.7beta2? > It's up to the release manager, but personally it feels like you're pushing too hard. Since you've done that frequently in the past I think we should help you by declining your request. 3.8 is right around the corner (on a Python release timescale) and this will give people a nice incentive to upgrade. Subsequent betas exist to stabilize the release, not to put in more new stuff.
- montalbano 6y agoThe same is true in Julia but for slightly different reasons. It's not just quicker variable lookup in Julia, but the fact that it will be JIT compiled. (Unlike CPython.)
- wwright 6y agoNot just because it's a lookup-by-name, but because globals are mutable everywhere, the interpreter has to assume that some other code has mutated the variable and has to re-fetch it in many cases.
- el_programmador 6y agoAnother 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...
- iandioch 6y agoThis is true. A related post about discovering this, and some links to more info: https://news.ycombinator.com/item?id=18681745 https://news.ycombinator.com/item?id=18681745