I gave a talk yesterday at PyCon 2013 called Loop Like a Native. The main point was: write more generators. Give it a look.

tagged: , » 4 reactions

Comments

[gravatar]
Ed Davies 6:40 AM on 17 Mar 2013

Thanks for posting this. I picked up a few useful hints; particularly I'd missed the rather handy enumerate function. Also, I'll be thinking more of using generators to flatten nested loops into a single loop.

[gravatar]
Lucian 8:26 AM on 17 Mar 2013

I really like your format for blog slides. So much more useful than just the slides, maybe even a video.

[gravatar]
Piotr Dobrogost 3:42 PM on 18 Mar 2013

To loop one has to have iterable in the first place whereas often we get scalar value instead. That's why I find it useful to have this kind of "adapter":

    def as_iterable(input,
                    scalar_types=(basestring,),
                    empty_types=(types.NoneType,)):
        if isinstance(input, empty_types):
            return ()
        else:
            if isinstance(input, scalar_types):
                return (input,)
            else:
                return iter(input)

[gravatar]
Alok Joshi 4:03 PM on 18 Mar 2013

Superb article/talk. Thanks.

Add a comment:

name
email
Ignore this:
not displayed and no spam.
Leave this empty:
www
not searched.
 
Name and either email or www are required.
Don't put anything here:
Leave this empty:
URLs auto-link and some tags are allowed: <a><b><i><p><br><pre>.