except clause not catching IndexError

Roy Smith roy at panix.com
Thu Feb 23 07:32:21 EST 2006


Steven D'Aprano <steve at REMOVEMEcyber.com.au> wrote:
> And here I was thinking that commas make tuples, not 
> brackets. What is happening here?

What is happening is that the syntax for forming tuples is one of Python's 
warts.  Sometimes the comma is what makes a tuple:

>>> a = 1, 2
>>> type (a)
<type 'tuple'>

Sometimes, it is the parens:

>>> b = ()
>>> type (b)
<type 'tuple'>

Sometimes the syntax is ambiguous and you need both.  This happens anytime 
you have a list of comma separated things, such as in the arguments to a 
function:

a = foo (1, 2)       # two integer arguments
b = foo ((a, 2))     # one tuple argument

The except clause of a try statement is one of those times.



More information about the Python-list mailing list