PEP 3107 and stronger typing (note: probably a newbie question)

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Wed Jun 27 07:39:35 EDT 2007


paul a écrit :
> Bruno Desthuilliers schrieb:
>> Stephen R Laniel a écrit :
>>> On Wed, Jun 20, 2007 at 09:41:09PM +0100, Michael Hoffman wrote:
>>>> If you asked Java programmers why you couldn't turn *off* Java's 
>>>> static type checking if you wanted to, you'd probably get a similar 
>>>> response.
>>> Perhaps it would help for me to explain what I'd like.
>>>
>>> Under both Perl and Python, I've found myself
>>> having/wanting to write things like so:
>>>
>>> def my_func( int_arg, str_arg ):
>>>     try:
>>>         int_arg = int( int_arg )
>>>         str_arg = str( str_arg )
>>>     except ValueError:
>>>         sys.stderr.write( "Args are not of the right type\n" )
>>>         sys.exit(1)
>>>
>>
>> Just a question : what will happen if you get rid of the try/except 
>> block ?-)
>>
> The error will remain unnoticed

Err... cf below, I think you misunderstood me.

> and the argument might survive another 
> few function calls but eventually fails deep down somewhere with:
> 
> TypeError: cannot concatenate 'str' and 'list' objects
> 
> then you have to examine the traceback and hope the real error is 
> visible somewhere (an argument not conforming to the specification of 
> the function prototype, or the lack thereof).
> 

I asked about getting rid of the try/except - not about getting rid of 
the calls to int and str:

def my_func( int_arg, str_arg ):
      int_arg = int( int_arg )
      str_arg = str( str_arg )
      # now proceed...

Now, what will happen with this ?

And no, it's not type checking - it's duck typing at work.



More information about the Python-list mailing list