PEP 318

Stephen Horne steve at ninereeds.fsnet.co.uk
Wed Mar 24 04:18:26 EST 2004


On Wed, 24 Mar 2004 18:15:42 +1200, "Greg Ewing (using
news.cis.dfn.de)" <ieyf4fu02 at sneakemail.com> wrote:

>Paul Rubin wrote:
>> The syntax can be extended, i.e. "def foo() as generator" looks to me
>> to be a lot more explicit than "def foo()" followed by having the
>> compiler search the function body for a yield statement in order
>> to decide if it's a generator.
>
>While I happen to agree that generators ought to be
>created using something other than a plain "def",
>it couldn't be done this way. Generators need to be
>compiled differently from the beginning -- you can't
>turn an ordinary function into a generator by
>wrapping it in anything.

I just assumed the intention was that the word 'generator' would be
special-cased in the 'as' clause - you can do pretty much anything as
long as you spot the need while it's still just syntax.

Beyond that, it is probably possible by using the parser module. Just
a bit of AST hackery really. Though having never actually used that
module, I can't be sure.


-- 
Steve Horne

steve at ninereeds dot fsnet dot co dot uk



More information about the Python-list mailing list