MacPython Logo from __future__ import *

buy music albums Silver Apples buy mp3 albums Tarrus Riley buy tracks mp3 Kravits buy Reaper albums mp3 buy Kravits albums music buy music Evita CD online albums mp3 Silver Apples download Madonna CD music buy tracks music Kravits download music albums Silver Apples

2003-11-24

for loops and empty iterables

Filed under: python — bob @ 2:11 pm

Just van Rossum and I were discussing the for:else:, while:else: and try:else: syntax on the MacPythonChannel this morning (morning for me, at least). I'm not sure if this is sufficiently covered elsewhere, but here is the pattern I use to check for empty iterables:

class NoValue:
    """A unique placeholder, since None is sometimes used in iterables"""

def doesSomethingWithIterable(iterable):
    x = NoValue
    for x in iterable:
        pass # something useful could go here ;)
    else:
        if x is NoValue:
            pass # do something because iterable was empty
        else:
            pass # do something because iterable was not empty

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

I'm WP-Hashcash. I eat spam.

Powered by WordPress