You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
vari: Int# I want to declare type of "i" explicitly. Intuition also says, that this var will be mutated with every iterationforiinrange(10):
passprint('done at:', i, 'iteration')
What we expect to get? Well, that the value of i will be 9. Actual behavior is:
"done at: 9425703 iteration"
I am aware that it was intentional change to allow:
foriinrange(10): passforiinrange(10): pass
but current behavior is much more unintuitive than previous (in Python analogical code will give 9) and prevents to be explicit about specifying type of this iterator variable. I don't know if it is currently possible to specify type of "i" inside loop definition, but if it is not possible, specifying in outer scope is much more reasonable in static-like languages than not specifying it at all.
One more thing to consider: performance. If keeping scope of iterator variable only to loop gives real benefits in terms of performance it might be reasonable to keep it as it is. If it is not a case, accessing variable from outer scope is much more welcome, in terms of intuition, practical use cases and compatibility.
Edit: yet another thing to add here: lack of consistency.
vars: Intvari: Intforiinrange(3):
s=i*2
In this example variable s will be mutated, variable i not.
So it seems to me, that for keeping compatibility, intuitive behavior and simply usage, variables in function scope should be visible outside, I hope that it won't hurt performance!
The text was updated successfully, but these errors were encountered:
This was noted as a known "sharp edge" in the sharp edges/roadmap document. The behaviour here is definitely subject to change, and what you suggested could make sense, but require the declaration of the iteration variable seems not so great. Maybe syntax like for var i in range(3) but I don't have an opinion on the syntax for that.
Mogball
changed the title
[BUG]: Changed behavior to scope of iterator in for loop breaks compatibility with Python and acts unintuitive
[BUG] Changed behavior to scope of iterator in for loop breaks compatibility with Python and acts unintuitive
Jun 6, 2023
Python currently has not the possibility to express that, but for system (static) programming it should be a possibility. Maybe sth like this:
`for var [Int]i in ..`
would be the most pythonic?
There should be a way to express type of this iterator, because in Python during looping iterator could be of heterogeneous type (AnyType for non trivial examples). So compiler could not have the opportunity to perform fast path.
Bug description
Let consider some basic example:
What we expect to get? Well, that the value of
i
will be 9. Actual behavior is:"done at: 9425703 iteration"
I am aware that it was intentional change to allow:
but current behavior is much more unintuitive than previous (in Python analogical code will give
9
) and prevents to be explicit about specifying type of this iterator variable. I don't know if it is currently possible to specify type of "i" inside loop definition, but if it is not possible, specifying in outer scope is much more reasonable in static-like languages than not specifying it at all.One more thing to consider: performance. If keeping scope of iterator variable only to loop gives real benefits in terms of performance it might be reasonable to keep it as it is. If it is not a case, accessing variable from outer scope is much more welcome, in terms of intuition, practical use cases and compatibility.
Edit: yet another thing to add here: lack of consistency.
In this example variable
s
will be mutated, variablei
not.So it seems to me, that for keeping compatibility, intuitive behavior and simply usage, variables in function scope should be visible outside, I hope that it won't hurt performance!
The text was updated successfully, but these errors were encountered: