is parameter an iterable?

Rick Wotnaz desparn at wtf.com
Wed Nov 16 19:27:49 CET 2005


"Fredrik Lundh" <fredrik at pythonware.com> wrote in
news:mailman.750.1132159667.18701.python-list at python.org: 

> Rick Wotnaz wrote.
> 
>> ... which leads me to belive that 'msg' is not type(str). It
>> can be coerced (str(msg).find works as expected). But what
>> exactly is msg? It appears to be of <type 'instance'>, and does
>> not test equal to a string.
> 
> it's an instance of the exception type, of course.
> 
>:::
> 
> if you do
> 
>     raise SomeError, value
> 
> Python will actually do
> 
>     raise SomeError(value)
> 
> (that is, create a SomeError exception and pass the value as its
> first argument).
> 
> you can use either form in your code (I prefer the latter
> myself). 
> 
>:::
> 
> as for catching the exceptions, if you do
> 
>     try:
>         ...
>     except SomeError, v:
>         ...
> 
> Python will treat this as
> 
>     try:
>         ...
>     except:
>         # some exception occurred
>         typ = sys.exc_type
>         exc = sys.exc_value
>         if issubclass(typ, SomeError):
>             v = exc
>             ...
>         else:
>             raise # propagate!
> 
> (where typ and exc are internal variables)
> 

Thank you (and Roy Smith) for helping to clarify this. I see that 
my mental image of an Exception (which, I admit, was not based on 
extensive R'ing of TFM) was way off. Judging by Steven D'Aprano's 
code sample, I'm not the only one who was mistaken about the nature 
of  v  in your example. I'd always assumed it was the human-
readable string associated with the TypeError. Wrong, I see. 

-- 
rzed



More information about the Python-list mailing list