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

Chris Mellon arkanes at gmail.com
Tue Jun 26 11:55:11 EDT 2007


On 6/26/07, paul <paul at subsignal.org> wrote:
> 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 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).
>

In the example given, not catching the example will provide *more*
information than the terrible exception handling performed. If you're
going to write thick boilerplate to log the values of your locals and
arguments around all your functions, I suggest that you not do that
and instead make use of the ehanced tracebacks in the cgitb module,
which will give you a stack trace with the values of the locals in
each stack frame.

The only reason to trap an exception is either to redirect the
exception (for example, I have a call in a thread which can't be
permitted to throw, so exceptions are caught and transfered elsewhere
for logging), or to correct the error that caused the exception.
Boilerplate like this is at best useless, and in the case of the
example given actively worse than simply not catching it at all.



More information about the Python-list mailing list