On 8/6/2013 6:50 AM, Chris Angelico wrote:
On Tue, Aug 6, 2013 at 11:28 AM, Rui Maciel <rui.mac...@gmail.com> wrote:
Chris Angelico wrote:
def add_three_values(x,y,z):
     return x+y+z

Do you want to test these values for compatibility? Remember, you
could take a mixture of types, as most of the numeric types can safely
be added. You can also add strings, or lists, but you can't mix them.
And look! It already raises TypeError if it's given something
unsuitable:

If the type problems aren't caught right away when the invalid types are
passed to a function then the problem may only manifest itself in some far
away point in the code, making this bug needlessly harder to spot and fix,
and making the whole ordeal needlessly too time consuming.

There are two problems that can result from not checking:

1) The traceback will be deeper and may be less clear.

2) Some code will be executed and then an exception thrown.

3) The code falls into an infinite loop or recursion.

The solution is to think before looping or recursing. This often involves value checking (non-negative int or non-fractional float, for instance) rather than type checking in the usual static type-checking sense.

One also needs to be careful about passing unbounded iterators to other functions and remember that unboundedness is contagious. (filter(pred, unbounded_iterator) is an unbounded iterator). Again, this is a 'value' or implicit sub-type issue rather than a explicit, visible 'type' issue.

Infinite recursion will be caught eventually when memory runs out (and that is an advantage of recursion over iteration ;-), but I prefer to avoid it anyway.

--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to