-4

I have seen a code written like this:

if __name__ == "__main__":
    #...
    result = someFunction(someParameter)
    sys.exit(result)

and even like this, where you write the call of the function in the sys.exit() function:

if __name__ == "__main__":
    #...
    sys.exit(someFunction(someParameter))

Which is the most correct option according to style?

Peter Badida
  • 11,310
  • 10
  • 44
  • 90
dreinoso
  • 1,479
  • 17
  • 26
  • 6
    That's not an alternative, it is the same thing, just that in the second one you did not create any variable. – ettanany Nov 25 '16 at 20:55
  • 1
    What if there is some code after all that (forget the `sys.exit()`) ? If you use a variable, its value is stored, what happen if you use the result of a function inline? is its returned value still in the stack after you had use of it? do you have to call `gc.collect()` ? an answer about python's memory management would be great. – Loïc Nov 25 '16 at 21:26
  • It's the same result, but not the same thing.. I wanted to know what was the best option according to style. Like freebie said, the most readable option is the correct, so the first. – dreinoso Nov 25 '16 at 21:29

1 Answers1

3

Both are equivalent, but I think you know this. If you are asking from a style perspective, I'd say you should always favour the option that is more readable. In this case, I think the first one is more readable.

Python uses snake_case by convention so you might consider also using that to define your variables and functions.

You might also want to change the name of the result variable to be more descriptive.

exit_status = some_function(some_parameter)
sys.exit(exit_status)

Hopefully, this was what you were after.

Edit: @AnttiHaapala made a good point that succeeded isn't the best variable name either as 0 is Falsy in Python but considered a pass as an exit code. I've renamed it to exit_status.

freebie
  • 2,161
  • 2
  • 19
  • 36