[Pyrex] [Cython-dev] req - block-local variables
Robert Bradshaw
robertwb at math.washington.edu
Sun Dec 9 11:39:16 CET 2007
On Dec 8, 2007, at 3:57 PM, Greg Ewing wrote:
> Robert Bradshaw wrote:
>> On Dec 8, 2007, at 12:07 PM, Sven Berkvens-Matthijsse wrote:
>>>> On Sat, 2007-12-08 at 10:03 +0100, Stefan Behnel (I think) wrote:
>>>>
>>>> I find that with large functions, it's distracting to keep flipping
>>>> between lines of code in the function and the cdef vars at the top
>>>> of the function.
>
> I would argue that if you can't fit them both on the screen
> at once, your function is too big and needs breaking up.
:-) I think this is true most of the time, but not always. Even so,
I'm a fan of keeping things as local as possible. If one doesn't need
a variable until halfway down the function (or only within a single
if block) I'd rather declare it there than further up.
>> Variables don't have to be defined at the top of a function, they
>> just (currently) can't be defined inside blocks. This restriction
>> could be removed (with leaking) fairly easily and would solve
>> this issue. What are the drawbacks to doing this.
>> Greg, I'm cc'ing you because I'm guessing you had some reason to
>> explicitly dis-allow this. Comments?
>
> Well, the "with leaking" is kind of a clue. Various aspects of
> the code generation assume that function-local variables live for
> the whole lifetime of the function call and only need to be
> cleaned up at the end. Changing that would require some careful
> thought, and there doesn't seem to be any strong motivation to
> try.
My thoughts exactly.
> Possibly it could be made legal to declare variables anywhere
> in the function, but still have them all at function scope.
> But that might be confusing to people expecting them to be
> block-scoped.
Thanks for the feedback. Since in Python, any local-variable
declaration is at function scope, I think block-scoped variables
could be confusing (for Python coders, which probably make up the
majority of the userbase) too. Right now "cdef statement not allowed
here" at least isn't confusing, but it can be frustrating (e.g. I
need a loop variable here, and it's annoying to have to go to the top
of the function to define it.)
If there aren't any objections, I'm thinking of loosening this
constraint for Cython.
- Robert
More information about the Pyrex
mailing list