(why) inconsistent yield/return syntax?

Mark McEahern marklists at mceahern.com
Mon Feb 10 16:11:14 EST 2003


[Gerrit Holl]
> An empty return is ok and return None, but an empty yield isn't.
> My question is why...

I'm curious whether you can state your question in terms of a goal that you
are trying to accomplish.

For instance:

"How do I define a zero-length iterator?"

#!/usr/bin/env python

from __future__ import generators

import unittest

def whynot():
    if True:
        raise StopIteration
    yield None

class test(unittest.TestCase):

    def test(self):
        """Our silly iterator is pointless, but so what."""
        l = [x for x in whynot()]
        self.assertEquals(len(l), 0)

if __name__ == '__main__':
    unittest.main()

Cheers,

// m

-






More information about the Python-list mailing list