Re: is_iterable function.

2007-07-26 Thread Neil Cerutti
On 2007-07-26, George Sakkis <[EMAIL PROTECTED]> wrote: > That's not the only problem; try a string element to see it > break too. More importantly, do you *always* want to handle > strings as iterables ? > > The best general way to do what you're trying to is pass > is_iterable() as an optional ar

Re: is_iterable function.

2007-07-26 Thread George Sakkis
On Jul 26, 2:56 pm, Neil Cerutti <[EMAIL PROTECTED]> wrote: > On 2007-07-26, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > > > > > On Thu, 26 Jul 2007 15:02:39 +, Neil Cerutti wrote: > > >> Based on the discussions in this thread (thanks all for your > >> thoughts), I'm settling for: >

Re: is_iterable function.

2007-07-26 Thread Neil Cerutti
On 2007-07-26, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > On Thu, 26 Jul 2007 15:02:39 +, Neil Cerutti wrote: > >> Based on the discussions in this thread (thanks all for your >> thoughts), I'm settling for: >> >> def is_iterable(obj): >> try: >> iter(obj).next() >> return

Re: is_iterable function.

2007-07-26 Thread Marc 'BlackJack' Rintsch
On Thu, 26 Jul 2007 15:02:39 +, Neil Cerutti wrote: > Based on the discussions in this thread (thanks all for your > thoughts), I'm settling for: > > def is_iterable(obj): > try: > iter(obj).next() > return True > except TypeError: > return False > except KeyError: > ret

Re: is_iterable function.

2007-07-26 Thread Neil Cerutti
Based on the discussions in this thread (thanks all for your thoughts), I'm settling for: def is_iterable(obj): try: iter(obj).next() return True except TypeError: return False except KeyError: return False The call to iter will fail for objects that don't support the iterat

Re: is_iterable function.

2007-07-26 Thread Bruno Desthuilliers
Neil Cerutti a écrit : > def is_iterable(obj): > try: > iter(obj) > return True > except TypeError: > return False > > Is there a better way? The only other alternative I see is worse: def iterable(obj): # strings are iterable and don't have an __iter__ method.

Re: is_iterable function.

2007-07-25 Thread Steve Holden
Carsten Haese wrote: > On Wed, 2007-07-25 at 19:26 +, Neil Cerutti wrote: >> Speaking of the iter builtin function, is there an example of the >> use of the optional sentinel object somewhere I could see? > > Example 1: If you use a DB-API module that doesn't support direct cursor > iteration

Re: is_iterable function.

2007-07-25 Thread Marc 'BlackJack' Rintsch
On Wed, 25 Jul 2007 15:46:14 -0400, Carsten Haese wrote: > On Wed, 2007-07-25 at 19:11 +, Marc 'BlackJack' Rintsch wrote: >> And just calling `iter()` doesn't work either: >> >> In [72]: class A: >>: def __getitem__(self, key): >>: if key == 42: >>:

Re: is_iterable function.

2007-07-25 Thread George Sakkis
On Jul 25, 3:26 pm, Neil Cerutti <[EMAIL PROTECTED]> wrote: > Speaking of the iter builtin function, is there an example of the > use of the optional sentinel object somewhere I could see? # iterate over random numbers from 1 to 10; use 0 as a sentinel to stop the iteration for n in iter(lambda:r

Re: is_iterable function.

2007-07-25 Thread Neil Cerutti
On 2007-07-25, Carsten Haese <[EMAIL PROTECTED]> wrote: > On Wed, 2007-07-25 at 19:26 +, Neil Cerutti wrote: >> Speaking of the iter builtin function, is there an example of the >> use of the optional sentinel object somewhere I could see? > > Example 1: If you use a DB-API module that doesn't

Re: is_iterable function.

2007-07-25 Thread Neil Cerutti
mapped(func, iterable): for item in iterable: try: for it in flattened(item): yield func(it) except TypeError: yield func(item) I'd be more confortable excepting some sort of IterationError (or using an is_iterable function, of course). I guess there's

Re: is_iterable function.

2007-07-25 Thread Duncan Booth
Neil Cerutti <[EMAIL PROTECTED]> wrote: > Speaking of the iter builtin function, is there an example of the > use of the optional sentinel object somewhere I could see? for line in iter(open('somefile.txt', 'r').readline, ''): print line -- http://mail.python.org/mailman/listinfo/python

Re: is_iterable function.

2007-07-25 Thread Carsten Haese
On Wed, 2007-07-25 at 19:26 +, Neil Cerutti wrote: > Speaking of the iter builtin function, is there an example of the > use of the optional sentinel object somewhere I could see? Example 1: If you use a DB-API module that doesn't support direct cursor iteration with "for row in cursor", you c

Re: is_iterable function.

2007-07-25 Thread Carsten Haese
On Wed, 2007-07-25 at 19:11 +, Marc 'BlackJack' Rintsch wrote: > And just calling `iter()` doesn't work either: > > In [72]: class A: >: def __getitem__(self, key): >: if key == 42: >: return 'answer' >: raise KeyError >:

Re: is_iterable function.

2007-07-25 Thread Jeff McNeil
That's not going to hold true for generator functions or iterators that are implemented on sequences without '__iter__.' You might also want to check for __getitem__. I'm not sure if there's a way to tell if a function is a generator without actually calling it. -Jeff On 7/25/07, [EMAIL PROTEC

Re: is_iterable function.

2007-07-25 Thread Neil Cerutti
On 2007-07-25, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > You can use the built-in dir() function to determine whether or not > the __iter__ method exists: I think you might have to check for __getitem__, also, which may support the sequence protocol. > def is_iterable(thing): > return '_

Re: is_iterable function.

2007-07-25 Thread Carsten Haese
On Wed, 2007-07-25 at 11:58 -0700, [EMAIL PROTECTED] wrote: > You can use the built-in dir() function to determine whether or not > the __iter__ method exists: > > class Iterable(object): > def __iter__(self): > pass > > class NotIterable(object): > pass > > def is_iterable(thing

Re: is_iterable function.

2007-07-25 Thread Marc 'BlackJack' Rintsch
On Wed, 25 Jul 2007 11:58:40 -0700, [EMAIL PROTECTED] wrote: > You can use the built-in dir() function to determine whether or not > the __iter__ method exists: Doesn't work: In [58]: is_iterable('hello') Out[58]: False But strings *are* iterable. And just calling `iter()` doesn't work either:

Re: is_iterable function.

2007-07-25 Thread [EMAIL PROTECTED]
You can use the built-in dir() function to determine whether or not the __iter__ method exists: class Iterable(object): def __iter__(self): pass class NotIterable(object): pass def is_iterable(thing): return '__iter__' in dir(thing) print 'list is iterable = ', is_iterable(l

is_iterable function.

2007-07-25 Thread Neil Cerutti
def is_iterable(obj): try: iter(obj) return True except TypeError: return False Is there a better way? -- Neil Cerutti -- http://mail.python.org/mailman/listinfo/python-list