Newbie: static typing?

Rui Maciel rui.maciel at gmail.com
Tue Aug 6 06:28:07 EDT 2013


Chris Angelico wrote:

> On Tue, Aug 6, 2013 at 10:01 AM, Rui Maciel <rui.maciel at gmail.com> wrote:
>> It would be nice if some functions threw an error if they were passed a
>> type
>> they don't support or weren't designed to handle.  That would avoid
>> having to deal with some bugs which otherwise would never happen.
>>
>> To avoid this sort of error, I've been testing arguments passed to some
>> functions based on their type, and raising TypeError when necessariy, but
>> surely there must be a better, more pythonic way to handle this issue.
> 
> 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.


Rui Maciel



More information about the Python-list mailing list