Show off your Python chops and compete with others

Chris Angelico rosuav at gmail.com
Thu Nov 7 21:26:21 EST 2013


On Fri, Nov 8, 2013 at 1:18 PM, Roy Smith <roy at panix.com> wrote:
> It's not a shifting goalpost.  My original statement was that:
>
> def foo():
>    raise Exception
>
> defines a function which 1) has no explicit return statement and 2) does
> not return None.  I stand by that statement.  There is no possible
> codepath, no possible calling sequence, no possible execution
> environment, which will cause that function to return None.  That fact
> that one particular Python implementation happens to produce unreachable
> bytecode for returning None is meaningless.  Would you say that:
>
> def baz():
>    return None
>    print "I got here"
>
> is a function which prints "I got here"?

Granted, but I would describe this:

def foo(x):
    return "Hello, world!\n" + str(x)

as a function which returns a string. Is it? Well, not if str raises
an exception. Even if the only arguments you can give to foo will
result in exceptions, I would still say that, per design, this is a
function that returns a string. The possibility of raising an
exception (and thus not returning anything) doesn't change a
function's return type (by which I mean more than just what C would
use as the declaration - I could just as well say "Returns the name of
an employee", and the same argument would apply).

ChrisA



More information about the Python-list mailing list