Hacker News new | ask | show | jobs
by aepiepaey 2463 days ago
As long as we're talking about libraries, yes.

For cli tools, on the other hand, there are definitely legitimate cases for exiting early.

1 comments

Sure, but even in most of those scenarios, I’d prefer to structure the code differently... perhaps write a main() function that just returns, or other functions... more modular, maintainable, teatable, ... admittedly, in Python you need to use os.exit if you want to return a specific non-zero exit code, but even in that case good code style would be something like

  def main()
    blablabla
    if foo:
      return bar
    ...
    return 8
  
  if __name__ == “__main__”:
    os.exit(main())
Of course that’s just my advice / opinion / preference, other ways are valid as well...