Python: Generator methods

This recipe enables the use of the yield statement within a method by decorating that method with a wrapper for a generator object.

Say what you will about Python, it does provide us with clever tricks such as this one.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I would have thought..

functional folks would be all over generators as evil imperative things. After all, with lazy lists and tail call optimization one can generate results from finite state machines just fine. and copy them at any point. I don't think a Python generator be copied?

and the Schemers should be yawning.

functional folks would be all over generators as evil imperative things.

It's not in Haskell, what do I care?

Anyways, there are much more imperative things in languages typically accepted as "functional". They even let you do this.

Yeah what he said...

If it makes pythonistas happy, good for them.

After ten years of daily use, I think I've grown out of Python. It doesn't do anything interesting to me nowadays. Haskell has a lot more above and beyond what Python offers. Maybe that's just how my brain works. At this point my daily paying Python code looks like Haskell.

Witness a snippet from today that would be simpler and more efficient with Haskell.
(portal.portal_languages.getLanguageCookie() or ([l for l in portal.portal_languages.getRequestLanguages() if l in portal.portal_languages.getSupportedLanguages()] or ['en'])[0])
(For the curious, it does language fallback for LinguaPlone)


--Shae Erisson - ScannedInAvian.com

I'm tired...

...so pardon the bugs:

portal.portal_languages.getLanguageCookie() or (portal.portal_languages.getRequestLanguages() and portal.portal_languages.getSupportedLanguages() + 'en')[0]