終了するスクリプトの制御をプロセスのどこに置くべきか疑問に思っていますか?
スクリプトを続行するかどうかを決定するために関数が使用されている場合、結果に基づいて、呼び出し元または呼び出し先で制御する必要がありますか?
いずれかになる可能性のあるシナリオはありますか?
(この質問にはより広い意味があると確信しているので、プログラミングのより高いレベルの実践に答えを自由に拡張してください。それは実際には素晴らしいことです)
条件付きスクリプトの終了のオプションと、制御を委任するかどうかのオプションとして検討するいくつかの例を以下にリストします。
should_continue
提供された引数が有効であり、スクリプトを続行するにはその有効性が必要であることを確認していると想像してください。それ以外の場合は終了します。
'''
ex 1: return state to parent process to determine if script continues
'''
def should_continue(bool):
if bool:
return True
else:
return False
def init():
if should_continue(True):
print 'pass'
else:
print 'fail'
'''
ex 2: return state only if script should continue
'''
def should_continue(bool):
if bool:
return True
else:
print 'fail'
sys.exit() # we terminate from here
def init():
if should_continue(True):
print 'pass'
'''
ex 3: Don't return state. Script will continue if should_continue doesn't cause termination of script
'''
def should_continue(bool):
if not bool:
print 'fail'
sys.exit()
def init():
should_continue(True)
print 'pass'