Re: is parameter an iterable?
Steven D'Aprano <steve@REMOVETH IScyber.com.au> wrote in
news:pan.2005.1 1.15.22.57.58.8 55137@REMOVETHI Scyber.com.au:
[color=blue]
> def foo(inputVal):
> try:
> for val in inputVal:
> # do stuff
> except TypeError, msg:
> if msg == "iteration over non-sequence":
> # handle non-iterable case
> else:
> # some other TypeError is a bug, so re-raise the
> exception raise[/color]
Does this in fact work on your system? On mine (2.4.1 (#65, Mar 30
2005, 09:13:57) [MSC v.1310 32 bit (Intel)]), it doesn't seem to. I
tried
if msg.find("itera tion over non-sequence") >= 0:
.... but I got a traceback, and
AttributeError: TypeError instance has no attribute 'find'
.... 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. This is not the least surprise to me.
--
rzed
Steven D'Aprano <steve@REMOVETH IScyber.com.au> wrote in
news:pan.2005.1 1.15.22.57.58.8 55137@REMOVETHI Scyber.com.au:
[color=blue]
> def foo(inputVal):
> try:
> for val in inputVal:
> # do stuff
> except TypeError, msg:
> if msg == "iteration over non-sequence":
> # handle non-iterable case
> else:
> # some other TypeError is a bug, so re-raise the
> exception raise[/color]
Does this in fact work on your system? On mine (2.4.1 (#65, Mar 30
2005, 09:13:57) [MSC v.1310 32 bit (Intel)]), it doesn't seem to. I
tried
if msg.find("itera tion over non-sequence") >= 0:
.... but I got a traceback, and
AttributeError: TypeError instance has no attribute 'find'
.... 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. This is not the least surprise to me.
--
rzed
Comment