This breaks nothing (in my experience), but lots of things "don't break anything" and are still wrong.
#! /usr/bin/env python
"""Short description of this module."""
def preferred_exit_method(*args):
#code for exiting as desired
try:
import foo
import bar
import eggs
except ImportError as e:
#code code code
preferred_exit_method(e)
So, clearly defining the program's behavior on exit, then using it during the import
process can't be too terrible, right? But it goes against the oft-cited PEP 8, which states that modules should be absolutely be imported before anything global.
It seems like an unrisky proposition, but then there's always something like this:
#! /usr/bin/env python
"""Description of module"""
def exit_of_choice(*args):
#code code code
sys.exit(0)
try:
import sys
import other_things
except ImportError as e:
exit_of_choice(e)
Apart from writing code that never needs to do this because it is perfect and never fails at anything, is there a reason besides PEP 8 and "other Python people will be cross with you" not to do this? (well, I'm less concerned with the latter - even though sys
wouldn't typically fail to import, I still sort of hate that on principle)