is_iterable function.

Neil Cerutti horpner at yahoo.com
Wed Jul 25 22:01:29 CEST 2007


On 2007-07-25, Carsten Haese <carsten at uniqsys.com> wrote:
> On Wed, 2007-07-25 at 19:26 +0000, 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 can simulate it this way:
>
> for row in iter(cursor.fetchone, None):
>    # do something
>
> Example 2: Reading a web page in chunks of 8kB:
>
> f = urllib.urlopen(url)
> for chunk in iter(lambda:f.read(8192), ""):
>   # do something

Ah! Thanks for the examples. That's much simpler than I was
imagining. It's also somewhat evil, but I suppose it conserves a
global name to do it that way.

-- 
Neil Cerutti



More information about the Python-list mailing list