Exceptions, assigning a tuple

Erik Max Francis max at alcyone.com
Fri Nov 21 02:25:33 EST 2003


Derek Fountain wrote:

> OK, that makes a lot of sense. However, the thing being unpacked here
> isn't
> a tuple - it's an object of type IOError. How does an object know how
> to
> unpack itself when used in an assignment like this? More specifically,
> how
> does the IOError object know to return its errno and strerror values
> when
> it gets assigned to a 2 value tuple?

Because the Exception class, which IOError is derived from, works that
way:

>>> e = Exception(1, 2, 3)
>>> tuple(e)
(1, 2, 3)
>>> x, y, z = e
>>> x 
1
>>> y
2
>>> z
3

The "exception parameter" is an exception object, which knows how to
behave like a tuple.

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ 
\__/ Yes I'm / Learning from falling down / Heavily
    -- Lamya




More information about the Python-list mailing list