[Cython] InlinedGeneratorExpression question

Stefan Behnel stefan_ml at behnel.de
Tue Mar 8 11:19:24 CET 2011

Vitja Makarov, 08.03.2011 11:01:
> Hi!
> Here is example code:
> def foo(seq):
>     cdef int x
>     return any(x for x in seq)
> Here inner x have type int, how does cdef affects nested scope?
> Is that correct?

Yes, that's intended. Otherwise there'd be no way to assign types to 
variables used in genexpr/comprehension scopes.

The rule is: if there is a variable of the same name cdef-ed in the outer 
scope, inherit its type.

Arguably, this leaves a dangling unused variable in the outer scope, but I 
find that a minor issue compared to the problem of declaring a type for the 
inner scope.


More information about the cython-devel mailing list