-
-
Notifications
You must be signed in to change notification settings - Fork 780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Episode 5 - Repeating Actions with Loops re-use variables previously defined as loop variables #974
Comments
I'm a member of The Carpentries Core Team and I'm submitting this issue on behalf of another member of the community. In most cases, I won't be able to follow up or provide more details other than what I'm providing below. As my final step in the checkout process, I will be commenting further on the issue raised by Intran26 on january 11th (#974) in the software carpentries python course. I completely agree with their position. There really are few cases where one would want to declare a variable outside of a loop, manipulate it within the loop, and then to use it again later. If anything, I feel this sidebar should be relocated further down into lesson 8-Creating functions as an aside during the discussion of scope. Variable scope is the most closely related idea to this loop usage, so if it were to be addressed at all and to avoid confusing learners, I would include it in a broader discussion on scope. But of course, as Intran26 mentioned, if there is a bigger reason for this example to be included that we are missing, please outline it in the text. Being the second person to comment on this issue as confusing for learners, I can't imagine that I will be the last. Clarifying the example, or relocating it to a more appropriate section to discuss variable scope, will help demystify this example. |
I can understand a need for more clarification about this example. In support of not removing it, however, my personal understanding was that it had a valuable purpose. We want learners to have an accurate mental model of how loops work, including the loop variables. It doesn't really matter whether that exact pattern is ever useful; if you don't understand why loop variables work that way, you're not understanding the loop in Python. ...It's also a common source of errors if that same variable name gets used later in the program. |
There's also one practical use for this idiom, such as when you're using the break command. If you want to search a list for the first occurrence of a value. |
In Episode 5 - Repeating Actions with Loops there is a line and an example discussing the re-use of the for loop variables outside of the for loop:
Note that a loop variable is a variable that is being used to record progress in a loop.
It still exists after the loop is over,
and we can re-use variables previously defined as loop variables as well:
{: .language-python}
I'm not sure if this "note" is a useful point for learners. In my experience, there isn't any practical use case for this behavior of Python (for loop variable existing outside the loop). It seems more like an artifact rather than a feature, and might give learners the wrong impression about the practicality of such usage without further clarification.
The accompanying code example seems to be illustrating a different point: cautionary usage of the same variable name outside and inside the for loop, with the intention that the variable "name" started with being 'Rosalind' but has changed its value to 'Turing' by the end of the for loop. But the text did not explain this.
If there are reasons for leaving this part in that I'm not aware of, I'd suggest a bit more clarification in the text about the value stored in the loop variable will be whatever the final value it was assigned to in the loop and you cannot use it to access any other values it was previously assigned to, as well as the danger of changing your variable's value unintentionally.
The text was updated successfully, but these errors were encountered: