[Cython] generators
Stefan Behnel
stefan_ml at behnel.de
Sat Jul 12 08:06:12 CEST 2008
Hi,
Robert Bradshaw wrote:
> On Jul 11, 2008, at 8:04 AM, Stefan Behnel wrote:
>> 1)
>>
>> def f():
>> x = 1
>> def a(): print x
>> def b(): print x
>> return (a,b)
>
> There will be a cdef class created for a and b, as well as one
> representing the scope of f, with a __call__ method containing the
> body of the function. The def a() and def b() lines would instantiate
> this class and assign them to local variable a and b (which have a
> reference to the containing scope). When both of these are collected,
> the scope will be as well.
Yes, that sounds correct to me.
>> 2)
>>
>> for i in range(10):
>> def a(): print i
>> a()
>>
>> (outputs 0-9 in Python)
>
> This isn't an inner function at all. To fix the scoping rules, "def"
> would create a function at the top level, and then do an assignment
> to the variable "a."
I actually meant it to be an inner function of a function, so this wasn't a
clear example. But I can see that this would be handled the same way as above.
So, yes, I'm happy with that implementation. I just came up with these
examples when I was discussing the topic with some people at EuroPython, and I
couldn't really tell them how we would handle that.
Stefan
More information about the Cython-dev
mailing list