0

Which coding-style is better / correct and 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 we know that type of arg is checked in fun_bottom function, just omit assertions in fun_middle and fun_top? Or maybe there's another solution?

EDIT #1
Ouch, I was misunderstood. I just used assert isinstance(arg, int) as an example. I'll rewrite the question:

Which one to 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 we know that argument is checked in bottom-most function, we make no assertions in middle- and 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
6
  • 3
    Don't assert on type anywhere. Learn to love duck typing, and rely on the methods being defined that you expect to be defined. Commented May 14, 2013 at 22:04
  • Why do you think you need to assert this at all? If you want to write functions that only accept declared argument types, do so in a statically typed language like Java. Otherwise, write Python the way it is meant to be written. Commented May 14, 2013 at 22:05
  • You need to use duck typing. Normalize the input in a try/except, then do whatever you need to do with it. Commented May 14, 2013 at 22:10
  • Oh, I thought using type assertion is good practice. Anyway, I gave isinstance(arg, int) just as an example. Let's swap it for, for example, assert arg > 0. How about now? Commented May 14, 2013 at 22:14
  • Type assertion is basically the opposite of duck typing. Assertions exist for specific debugging purposes, but they should never go in production code, and you should certainly never rely on them to do flow-control for you (since they are turned off if __debug__ is turned off). If you need to do explicit value/type/whatever checking, do it explicitly. Otherwise, just put try and except around things that you expect might fail. Commented May 14, 2013 at 22:30

1 Answer 1

2

I would suggest a more pythonic way of doing things would me more like:

def fun_fun(some_int): # function that takes hopefully an int/float
    try: # not sure if we got the correct value
        return_value = some_int + 4 % 4 # mathz
        return return_value # return mathz
    except TypeError: # if we didn't get an int/float we'd get this
        return None # So you can return None or do what you like

See: http://docs.python.org/2/tutorial/errors.html

EDIT:

Maybe you want:

def fun_bottom(arg):
    if arg > 0:
        #blah blah
    else:
        #foo

Assert isn't supposed to be used in the manor your wanting it to be, have a read of: http://wiki.python.org/moin/UsingAssertionsEffectively

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.