lundi 2 mars 2015

Is it good Python style to write a function that has no effect other than potentially raise exceptions?


Sometimes I find myself writing Python code that looks like this:



def check_stuff():
if condition1:
return "condition1" # These might be enum values, etc., instead of strings
if condition2:
return "condition2"

def func():
cs = check_stuff()
if "condition1" == cs:
raise Exception1()
if "condition2" == cs:
raise Exception2()
#...


Here, the return value of check_stuff() is only used to decide what exception to raise. Let's imagine that this is true of every time I call check_stuff(), probably because I only call it once. Would it be better style to just raise the exceptions in check_stuff itself?



def check_stuff2():
if condition1:
raise Exception1()
if condition2():
raise Exception2()

def func2():
check_stuff()
#...




Aucun commentaire:

Enregistrer un commentaire