is parameter an iterable?

2005-11-15 Thread py
I have function which takes an argument. My code needs that argument to be an iterable (something i can loop over)...so I dont care if its a list, tuple, etc. So I need a way to make sure that the argument is an iterable before using it. I know I could do... def foo(inputVal): if isinstance

is parameter an iterable?

2005-11-15 Thread py
I have function which takes an argument. My code needs that argument to be an iterable (something i can loop over)...so I dont care if its a list, tuple, etc. So I need a way to make sure that the argument is an iterable before using it. I know I could do... def foo(inputVal): if isinstance

Re: is parameter an iterable?

2005-11-15 Thread Dan Sommers
On 15 Nov 2005 11:01:48 -0800, "py" <[EMAIL PROTECTED]> wrote: > I have function which takes an argument. My code needs that argument > to be an iterable (something i can loop over)...so I dont care if its a > list, tuple, etc. So I need a way to make sure that the argument is an > iterable befo

Re: is parameter an iterable?

2005-11-15 Thread marduk
On Tue, 2005-11-15 at 11:01 -0800, py wrote: > I have function which takes an argument. My code needs that argument > to be an iterable (something i can loop over)...so I dont care if its a > list, tuple, etc. So I need a way to make sure that the argument is an > iterable before using it. I kno

Re: is parameter an iterable?

2005-11-15 Thread py
Dan Sommers wrote: > Just do it. If one of foo's callers passes in a non-iterable, foo will > raise an exception, and you'll catch it during testing That's exactly what I don't want. I don't want an exception, instead I want to check to see if it's an iterableif it is continue, if not return

Re: is parameter an iterable?

2005-11-15 Thread Jean-Paul Calderone
On 15 Nov 2005 11:26:23 -0800, py <[EMAIL PROTECTED]> wrote: >Dan Sommers wrote: >> Just do it. If one of foo's callers passes in a non-iterable, foo will >> raise an exception, and you'll catch it during testing > >That's exactly what I don't want. I don't want an exception, instead I >want to c

Re: is parameter an iterable?

2005-11-15 Thread Rocco Moretti
marduk wrote: > On Tue, 2005-11-15 at 11:01 -0800, py wrote: > >>I have function which takes an argument. My code needs that argument >>to be an iterable (something i can loop over)...so I dont care if its a >>list, tuple, etc. So I need a way to make sure that the argument is an >>iterable befo

Re: is parameter an iterable?

2005-11-15 Thread Robert Kern
py wrote: > Dan Sommers wrote: > >>Just do it. If one of foo's callers passes in a non-iterable, foo will >>raise an exception, and you'll catch it during testing > > That's exactly what I don't want. I don't want an exception, instead I > want to check to see if it's an iterableif it is co

Re: is parameter an iterable?

2005-11-15 Thread Carl Friedrich Bolz
Hi! py wrote: > Dan Sommers wrote: > >>Just do it. If one of foo's callers passes in a non-iterable, foo will >>raise an exception, and you'll catch it during testing > > > That's exactly what I don't want. I don't want an exception, instead I > want to check to see if it's an iterableif

Re: is parameter an iterable?

2005-11-15 Thread py
Thanks for the replies. I agree with Jean-Paul Calderone's suggestion...let the exception be raised. Thanks. -- http://mail.python.org/mailman/listinfo/python-list

Re: is parameter an iterable?

2005-11-15 Thread Roy Smith
In article <[EMAIL PROTECTED]>, py <[EMAIL PROTECTED]> wrote: >I have function which takes an argument. My code needs that argument >to be an iterable (something i can loop over)...so I dont care if its a >list, tuple, etc. My first thought was to just write your loop inside a try block and catch

Re: is parameter an iterable?

2005-11-15 Thread Grant Edwards
On 2005-11-15, py <[EMAIL PROTECTED]> wrote: > Dan Sommers wrote: >> Just do it. If one of foo's callers passes in a non-iterable, foo will >> raise an exception, and you'll catch it during testing > > That's exactly what I don't want. I don't want an exception, instead I > want to check to see i

Re: is parameter an iterable?

2005-11-15 Thread Dave Hansen
On 15 Nov 2005 11:26:23 -0800 in comp.lang.python, "py" <[EMAIL PROTECTED]> wrote: >Dan Sommers wrote: >> Just do it. If one of foo's callers passes in a non-iterable, foo will >> raise an exception, and you'll catch it during testing > >That's exactly what I don't want. I don't want an exceptio

Re: is parameter an iterable?

2005-11-15 Thread lmaycotte
Maybe this helps: >>> import types >>> def foo(inputVal): inValType = type(inputVal) if inValType==types.ListType or inValType==types.TupleType: for val in inputVal: print val else: print 'Wrong input Type' >>> list = [1,2,3,4] >>> foo(list) 1 2 3 4 >>

Re: is parameter an iterable?

2005-11-15 Thread Steven D'Aprano
On Tue, 15 Nov 2005 14:06:45 -0500, Dan Sommers wrote: > On 15 Nov 2005 11:01:48 -0800, > "py" <[EMAIL PROTECTED]> wrote: > >> I have function which takes an argument. My code needs that argument >> to be an iterable (something i can loop over)...so I dont care if its a >> list, tuple, etc. So

Re: is parameter an iterable?

2005-11-15 Thread DonQuixoteVonLaMancha
How about hasattr("__iter__")? Regards, Karsten. -- http://mail.python.org/mailman/listinfo/python-list

Re: is parameter an iterable?

2005-11-15 Thread Ben Finney
[EMAIL PROTECTED] wrote: > Maybe this helps: > > >>> import types > >>> def foo(inputVal): > inValType = type(inputVal) > if inValType==types.ListType or inValType==types.TupleType: And what of user-created types that are iterable? What of user-created iterable types that don't inher

Re: is parameter an iterable?

2005-11-15 Thread Steven D'Aprano
On Tue, 15 Nov 2005 11:26:23 -0800, py wrote: > Dan Sommers wrote: >> Just do it. If one of foo's callers passes in a non-iterable, foo will >> raise an exception, and you'll catch it during testing > > That's exactly what I don't want. I don't want an exception, instead I > want to check to se

Re: is parameter an iterable?

2005-11-15 Thread Alex Martelli
Roy Smith <[EMAIL PROTECTED]> wrote: ... > TypeError: iteration over non-sequence > > I was kind of hoping for a more specific exception than TypeError. > You can't tell the difference between: > > try: > for i in 5: > print i + 1 > except TypeError: > print "non-iterable" > > and >

Re: is parameter an iterable?

2005-11-15 Thread Steven D'Aprano
Roy Smith wrote: > You can't tell the difference between: > > try: > for i in 5: > print i + 1 > except TypeError: > print "non-iterable" > > and > > try: > for i in ["one", "two", "three"]: > print i + 1 > except TypeError: > print "can't add string and integer" try: for

Re: is parameter an iterable?

2005-11-15 Thread Steven D'Aprano
[EMAIL PROTECTED] wrote: > Maybe this helps: Unfortunately, it doesn't. If you read the sample code the original poster first gave, you will see that Py's example is virtually the same as yours: [original code] def foo(inputVal): if isinstance(inputVal, (list, tuple)): for val in

Re: is parameter an iterable?

2005-11-15 Thread Dan Sommers
On Wed, 16 Nov 2005 13:12:38 +1100, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > try: > for item in obj: > do_stuff(item) > except TypeError, msg: > if msg == "iteration over non-sequence": > handle_non_iterator() > else: > # re-raise the exception >

Re: is parameter an iterable?

2005-11-15 Thread Roy Smith
In article <[EMAIL PROTECTED]>, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > try: > for item in obj: > do_stuff(item) > except TypeError, msg: > if msg == "iteration over non-sequence": > handle_non_iterator() > else: > # re-raise the exception >

Re: is parameter an iterable?

2005-11-16 Thread Steven D'Aprano
On Tue, 15 Nov 2005 21:54:07 -0500, Dan Sommers wrote: > On Wed, 16 Nov 2005 13:12:38 +1100, > Steven D'Aprano <[EMAIL PROTECTED]> wrote: > >> try: >> for item in obj: >> do_stuff(item) >> except TypeError, msg: >> if msg == "iteration over non-sequence": >> handle_non

Re: is parameter an iterable?

2005-11-16 Thread Steven D'Aprano
On Tue, 15 Nov 2005 21:59:44 -0500, Roy Smith wrote: > In article <[EMAIL PROTECTED]>, > Steven D'Aprano <[EMAIL PROTECTED]> wrote: > >> try: >> for item in obj: >> do_stuff(item) >> except TypeError, msg: >> if msg == "iteration over non-sequence": >> handle_non_iter

Re: is parameter an iterable?

2005-11-16 Thread Rick Wotnaz
Steven D'Aprano <[EMAIL PROTECTED]> wrote in news:[EMAIL PROTECTED]: > def foo(inputVal): > try: > for val in inputVal: > # do stuff > except TypeError, msg: > if msg == "iteration over non-sequence": > # handle non-iterable case > else: >

Re: is parameter an iterable?

2005-11-16 Thread Roy Smith
In article <[EMAIL PROTECTED]>, Rick Wotnaz <[EMAIL PROTECTED]> wrote: > Steven D'Aprano <[EMAIL PROTECTED]> wrote in > news:[EMAIL PROTECTED]: > > > def foo(inputVal): > > try: > > for val in inputVal: > > # do stuff > > except TypeError, msg: > > if msg ==

Re: is parameter an iterable?

2005-11-16 Thread Fredrik Lundh
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 , and does not test equal to a > string. it's an instance of the exception type, of course. ::: if you do rai

Re: is parameter an iterable?

2005-11-16 Thread Rick Wotnaz
"Fredrik Lundh" <[EMAIL PROTECTED]> wrote in news:[EMAIL PROTECTED]: > 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 , and does >> not test equal to a strin

Re: is parameter an iterable?

2005-11-16 Thread Steven D'Aprano
On Wed, 16 Nov 2005 09:06:01 -0500, Rick Wotnaz wrote: [cutting to the important bit] >> except TypeError, msg: >> if msg == "iteration over non-sequence": >> # handle non-iterable case > Does this in fact work on your system? On mine (2.4.1 (#65, Mar 30 > 2005, 09:13:57)

Re: is parameter an iterable?

2005-11-16 Thread Fredrik Lundh
Steven D'Aprano wrote: > This means we're no longer assuming what the error message will be, > which makes our code a lot more future-proof and implementation-proof: if > some version of Python changes the error string from "iteration over > non-sequence" to something else, the code should continu

Re: is parameter an iterable?

2005-11-16 Thread Fredrik Lundh
> Alex has already posted the right way to do this. can you please stop posting crappy non-solutions to a problem that has a very simple solution (split things up), that should be obvious to anyone who didn't sleep through exceptions 101. -- http://mail.python.org/mailman/listinfo/python-l

Re: is parameter an iterable?

2005-11-16 Thread Bengt Richter
On Wed, 16 Nov 2005 17:39:57 +0100, "Fredrik Lundh" <[EMAIL PROTECTED]> wrote: >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 , and does not test equal to a >

Re: is parameter an iterable?

2005-11-16 Thread Roy Smith
Steven D'Aprano <[EMAIL PROTECTED]> wrote: ># Create an instance of the exception you expect: >try: >for i in 0: >pass >except TypeError, ITER_OVER_NON_SEQ: >pass ># Now run your code... >try: >...blah blah blah... >except TypeError, msg >if str(msg) == str(ITER_OVER_NON_SE

Re: is parameter an iterable?

2005-11-16 Thread Steven D'Aprano
Fredrik Lundh wrote: > Steven D'Aprano wrote: > > >>This means we're no longer assuming what the error message will be, >>which makes our code a lot more future-proof and implementation-proof: if >>some version of Python changes the error string from "iteration over >>non-sequence" to something

Re: is parameter an iterable?

2005-11-17 Thread Steven D'Aprano
Ah, found the right solution! Thanks to Fredrik Lundh for so gently pointing me at this post. Just one question, if I may:- Alex Martelli wrote: > It's not hard...: > > try: > _it = iter(whatever) > except TypeError: > print 'non-iterable' > else: > for i in _it: # etc, etc Alas

Re: is parameter an iterable?

2005-11-17 Thread Steven D'Aprano
Roy Smith wrote: > Now you're assuming that the message is a constant for all TypeErrors > caused by attempting to iterate over a non-iterable object. Yes I am, and of course you are correct that that could change and so shouldn't be relied on. Seems that my crappy solution is not quite as fut

Re: is parameter an iterable?

2005-11-17 Thread Duncan Booth
Steven D'Aprano wrote: > What should I do when I can't rely on functions that > don't exist in older versions of Python? > Ideally: if 'iter' not in dir(__builtins__): import sys sys.exit('Archaic Python not supported, please upgrade') Alternatively fill in the blanks with app

Re: is parameter an iterable?

2005-11-17 Thread Roy Smith
Steven D'Aprano <[EMAIL PROTECTED]> wrote: > Alas and alack, I have to write code which is backwards > compatible with older versions of Python: > [...] > NameError: name 'iter' is not defined > > What should I do when I can't rely on functions that > don't exist in older versions of Python? I

Re: is parameter an iterable?

2005-11-17 Thread Micah Elliott
On Nov 17, Duncan Booth wrote: > Steven D'Aprano wrote: > > What should I do when I can't rely on functions that > > don't exist in older versions of Python? > sys.exit('Archaic Python not supported, please upgrade') +1 QOTW. I recently gave up on trying to support (backport to) pre-2.2 i

Re: is parameter an iterable?

2005-11-17 Thread Roy Smith
Micah Elliott <[EMAIL PROTECTED]> wrote: > I recently gave up on trying to support (backport to) pre-2.2 in my > projects. It's been ~3 years since 2.2 released and that seem like a > pretty reasonable support window. It depends on what you're doing. If you're developing for in-house use, you h

Re: is parameter an iterable?

2005-11-17 Thread Steve Holden
Micah Elliott wrote: > On Nov 17, Duncan Booth wrote: > >>Steven D'Aprano wrote: >> >>>What should I do when I can't rely on functions that >>>don't exist in older versions of Python? > > >> sys.exit('Archaic Python not supported, please upgrade') > > > +1 QOTW. > > I recently gave up o

Re: is parameter an iterable?

2005-11-21 Thread Fredrik Lundh
Steven D'Aprano wrote: > Alas and alack, I have to write code which is backwards > compatible with older versions of Python: > > Python 2.1.1 (#1, Aug 25 2001, 04:19:08) > [GCC 3.0.1] on sunos5 > Type "copyright", "credits" or "license" for more > information. > >>> iter > Traceback (most recent

Backwards compatibility [was Re: is parameter an iterable?]

2005-11-21 Thread Steven D'Aprano
Fredrik Lundh wrote: > Steven D'Aprano wrote: > > >>Alas and alack, I have to write code which is backwards >>compatible with older versions of Python: [snip] >>What should I do when I can't rely on functions that >>don't exist in older versions of Python? > > > python 2.1 doesn't support it

Re: Backwards compatibility [was Re: is parameter an iterable?]

2005-11-21 Thread Alex Martelli
Steven D'Aprano <[EMAIL PROTECTED]> wrote: ... > In the specific case of iter(), are there good > alternative ways of detecting an iterable without > consuming it? Not a problem I've had often, but when I did, if I recall correctly, I did something like: try: iter except NameError: def i

Re: Backwards compatibility [was Re: is parameter an iterable?]

2005-11-21 Thread Steven D'Aprano
Alex Martelli wrote: > "Consuming" didn't really come into consideration for the > backwards compatibility part because only objects indexable with > integers, 0 and up (and raising IndexError at some point) were usable in > for statements in old Pythons, there was no "consuming". Ah yes, of cour

Re: Backwards compatibility [was Re: is parameter an iterable?]

2005-11-21 Thread Fredrik Lundh
Alex Martelli wrote: > > In the specific case of iter(), are there good > > alternative ways of detecting an iterable without > > consuming it? > > Not a problem I've had often, but when I did, if I recall correctly, I > did something like: > > try: > iter > except NameError: > def isiterable(

Re: Backwards compatibility [was Re: is parameter an iterable?]

2005-11-22 Thread Tom Anderson
On Tue, 22 Nov 2005, Steven D'Aprano wrote: > Are there practical idioms for solving the metaproblem "solve problem X > using the latest features where available, otherwise fall back on older, > less powerful features"? > > For instance, perhaps I might do this: > > try: >built_in_feature >

Re: Backwards compatibility [was Re: is parameter an iterable?]

2005-11-23 Thread Fredrik Lundh
Tom Anderson wrote: > How about detecting which environment you're in, then running one of two > entirely different sets of code? Rather than trying to construct modern > features in the antique environment, write code for each, using the local > idioms. The trouble with this is that you end up wi

Re: Backwards compatibility [was Re: is parameter an iterable?]

2005-11-23 Thread Roy Smith
"Fredrik Lundh" <[EMAIL PROTECTED]> wrote: > I've said it many times, and I'll say it again: the only fundamentally > new concept that has been added since Python 1.5.2 is generators. > [...] > All the rest is just coloured frosting In my mind, the biggest thing since 1.5.2 is string methods. T