[Tutor] anecdote in the workplace about try,except clause

Remco Gerlich scarblac@pino.selwerd.nl
Fri, 17 Aug 2001 08:58:37 +0200

On  0, Sean 'Shaleh' Perry <shalehperry@home.com> wrote:
> So, thought i would pass this on.
> A coworker who is a long time perl'er is learning python and asking me
> questions now and then.  Today he was writing a file parser and did the
> following:
> for line in file:
>     try:
>         data, garbage = string.split(line, ':')
>     except ValueError:
>         pass
>     print 'Data: %s' % data
> His rationale was that some lines lack the ':' so he would just let the
> exception hide the 'annoying' python exception assuming that data would be
> assigned the whole line in that case.
> What he failed to realize is that when the except clause is triggered items in
> the try clause go out of scope.  So data would not exist.

try: doesn't have it's own scope, so data doesn't go out of scope. But it
was never assigned to in that loop.

I'd expect the print to print the data from the previous run through the loop.

Remco Gerlich