python - Checking if function was called with right arguments -
which coding-style better / correct , why? using assert statement in each function:
def fun_bottom(arg): assert isinstance(arg, int) #blah blah def fun_middle(arg): assert isinstance(arg, int) fun_bottom(arg) #blah blah def fun_top(arg): assert isinstance(arg, int) fun_middle(arg) #blah blah
or, because know type of arg checked in fun_bottom function, omit assertions in fun_middle , fun_top? or maybe there's solution?
edit #1
ouch, misunderstood. used assert isinstance(arg, int) example. i'll rewrite question:
which 1 use:
option 1: check if argument fulfil function's requirements in each function:
def fun_bottom(arg): assert arg > 0 #blah blah def fun_middle(arg): assert arg > 0 fun_bottom(arg) #blah blah def fun_top(arg): assert arg > 0 fun_middle(arg) #blah blah
option 2: because know argument checked in bottom-most function, make no assertions in middle- , top- functions:
def fun_bottom(arg): assert arg > 0 #blah blah def fun_middle(arg): fun_bottom(arg) #blah blah def fun_top(arg): fun_middle(arg) #blah blah
i suggest more pythonic way of doing things me more like:
def fun_fun(some_int): # function takes int/float try: # not sure if got correct value return_value = some_int + 4 % 4 # mathz return return_value # return mathz except typeerror: # if didn't int/float we'd return none # can return none or
see: http://docs.python.org/2/tutorial/errors.html
edit:
maybe want:
def fun_bottom(arg): if arg > 0: #blah blah else: #foo
assert isn't supposed used in manor wanting be, have read of: http://wiki.python.org/moin/usingassertionseffectively
Comments
Post a Comment