[portland] Need Help With a For Loop

kirby urner kirby.urner at gmail.com
Sat Mar 22 00:19:03 CET 2008


Jason wrote:

>  On the topic of iterator-based solutions, I really enjoyed Jim Baker's
>  "More Iterators In Action" talk at PyCon this year:
>
>    http://us.pycon.org/2008/conference/schedule/event/75/
>

Plus iterators in general moving into the foreground with
Python 3.x, in that many built-ins that used to return lists,
like range and adict.keys() will now return iterables,
which are more like generators.

Exploring in Python 3.0 a2:

IDLE 3.0a2

>>> t = {"and":None,"now":None,"for":None,"something":None,"completely":None,"different":None}.keys()

>>> type(t)  # Python 2.x you'd get back a list type here
<type 'dict_keys'>

>>> next(t)  # oops, can't pretend it's a generator yet
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    next(t)
TypeError: dict_keys object is not an iterator

>>> oops = iter(t)
>>> next(oops)  # ah, now we can
'and'
>>> help(iter)
Help on built-in function iter in module builtins:

iter(...)
    iter(collection) -> iterator
    iter(callable, sentinel) -> iterator

    Get an iterator from an object.  In the first form, the argument must
    supply its own iterator, or be a sequence.
    In the second form, the callable is called until it returns the sentinel.

>>> dir(t)  # note:  no __next__ method...
['__and__', '__class__', '__contains__', '__delattr__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__gt__',
'__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__',
'__ne__', '__new__', '__or__', '__rand__', '__reduce__',
'__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__',
'__setattr__', '__str__', '__sub__', '__xor__']

>>> type(oops)  # ...you'll find it'll have one
<type 'dict_keyiterator'>

>  Rumor has it that the PSF will post video of all of this year's talks,
>  so you'll be able to catch Jim's session if you missed it. (And Kirby's
>  and my sessions too.)

I walked away from the podium a lot, camera not tracking.  I'm
fortunate to be getting some post production services from Ian
Benson's sociality.tv -- presumably a slicker version of my talk is in
the pipeline (looking forward to seeing).

Wanna see Jim's and Jason's.

I also recommend the Unicode talk when it comes out too, was just
praising it on edu-sig:

http://farmdev.com/talks/unicode/

>
>  And there is audio of Jim's original "Iterators in Action" talk from
>  last year available in the PyCon 2007 podcasts:
>
>    http://advocacy.python.org/podcasts/
>

Muchas gracias!

Kirby

>  -j
>
>
> _______________________________________________
>  Portland mailing list
>  Portland at python.org
>  http://mail.python.org/mailman/listinfo/portland
>


More information about the Portland mailing list