"sins" (aka, acknowledged language problems)

Tim Peters tim_one at email.msn.com
Thu Dec 30 01:09:11 EST 1999


[John Skaller]
> try: ...
> except TypeError, x: print x
>
> Here, the 'x' is the actual exception object.
> It is not in global scope. It is not in local scope.
> At least, not in my implementation (Viper).
> It is available ONLY in the exception handler.
> At least, I assumed that: I never bothered to check this :-)

[Alex Martelli]
> You're right, I'm outmatched in the arrogance game...:
>
> >>> try:
> ...  raise IndexError, "hello"
> ... except IndexError, x:
> ...  print x
> ...
> hello
> >>> x
> <exceptions.IndexError instance at 796140>
> >>>
>
> It seems to _me_, as a humble (:-) language newbie, that this x
> is in no strange and magical scope, but, rather, exactly in
> the scope I would have expected it to be (here, typing things
> into the interactive interpreter, the global one).

It's actually in the local namespace, although at module level (including
__main__'s, to which you're typing in interactive mode) the global and local
namespaces are the same.  This is all spelled out in the "Code blocks,
execution frames, and namespaces" section of the Lang Ref (currently section
4.1).  It follows immediately from the general rule that binding targets are
*always* in the local namespace, unless explictly declared to be in the
global one.

That John could assume otherwise just goes to show that he thinks Python's
scheme is complicated because he's still trying to force some other
languages' view(s) onto it.  Forcing a bloated square peg into a slimly
elegant round hole is indeed a complicated business <wink>.

> ...
> Or, is it an issue of striking a happy medium?

I'm afraid The Amazing Criswell died before we got the chance to test this
theory on his candy ass.

suspecting-the-only-happy-mediums-are-fake-ones-ly y'rs  - tim






More information about the Python-list mailing list