Q about tail recursion

Tim Peters tim_one at email.msn.com
Sun Feb 27 16:40:46 EST 2000


[Robin Becker]
> I quite often want to use None as an exceptional return eg in the case
> of a list returning function [] might be a legal return and None an
> illegal one. That way I can leave the error handling to the caller.

Moshe & Michael later noted that CPython happens to generate different code
today for an explicit "return None" than for one it makes up on your behalf,
so not a problem after all.

> I guess I should really be raising an exception though to be truly
> pythonian.

Hard to say.  Half the world is ticked off that open("file") raises an
exception if "file" doesn't exist, and the other half is ticked off that
string.find(haystack, needle) doesn't raise an exception if needle isn't
found.  Rule of thumb is to raise an exception iff the condition is most
often viewed as "an error" (because programmers are notoriously  disinclined
to explicitly check for errors, so let trouble propagate).  In your specific
case, a careless caller who expects a list but gets back None is likely to
get an exception (probably TypeError or AttributeError) soon after anyway.

python-won't-let-you-live-but-lets-choose-how-to-die-ly y'rs  - tim






More information about the Python-list mailing list