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

Confusing variable name in "Programming with Python : Repeating actions with loops" #568

Open
donnaaboise opened this Issue Jul 18, 2018 · 12 comments

Comments

Projects
None yet
4 participants
@donnaaboise

donnaaboise commented Jul 18, 2018

In the beginning lesson "Programming with Python : Repeating actions with loops', the following example is provided :

word = 'lead'
for char in word:
    print(char)

This choice of loop index variable char is a confusing choice, since it is also a the name of a type used in compiled languages such as C or Fortran. It would be much better to choose a name that is more useful as a loop variable, i.e. a single letter name. I propose the following:

word = 'lead'
for c in word:
    print(c)

Or a more instructive option that doesn't conflict with compiled languages is

word = 'lead'
for letter in word:
    print(letter)
@abostroem

This comment has been minimized.

Contributor

abostroem commented Jul 18, 2018

Great point! Part of the motivation of the Python lesson is to teach best practices, including using descriptive variable names. How would you feel about using 'character' rather than c?

@maxim-belkin

This comment has been minimized.

Collaborator

maxim-belkin commented Jul 18, 2018

character or letter seems fine. (char is fine by me, TBH, but I'm fine with changing it).
What about word though? 🙄

@donnaaboise

This comment has been minimized.

donnaaboise commented Jul 18, 2018

In fact, character is the char type in Fortran, so this doesn't strike me as a good choice either.

Descriptive variable names are good in principle, but for loop indices, aren't shorter single letter variable names more the standard, especially if the value taken on by the loop variable is a scalar, i.e. integer (i) , float or double (x, y), character (s, c), and so on. So in this case, c seems more appropriate to me.

But I also like letter, for a more descriptive name.

I am all for descriptive variable names in an example such as:

basket = ['apple', 'orange', 'banana']
for fruit in basket:
    print(fruit)

In fact, I'd probably illustrate the example with something other than printing out a characters in a word).

@donnaaboise

This comment has been minimized.

donnaaboise commented Jul 18, 2018

word is okay, given the example. But I'd probably have gone for an entirely different example.

@maxim-belkin

This comment has been minimized.

Collaborator

maxim-belkin commented Jul 18, 2018

why doesn't word confuse you?

@donnaaboise

This comment has been minimized.

donnaaboise commented Jul 18, 2018

I am not confused by any of it, but am just thinking about the beginning Python programmer who may have had some exposure to compiled languages (maybe as a first year student?)

The choice of word is okay because in the example, the object really is a "word". I suppose one could also use string, but then the example gets really abstract.

@abostroem

This comment has been minimized.

Contributor

abostroem commented Jul 18, 2018

I like for letter in word. I think string is getting into python standard library messiness.

@maxim-belkin

This comment has been minimized.

Collaborator

maxim-belkin commented Jul 18, 2018

I see. Then I'd be more inclined to either keep the example "as is" or change "char" to "letter" (or anything "more descriptive", rather than "c"). The reason being that if a learner had prior exposure to compiled languages, she or he should relatively quickly understand what is happening in Python.

Please feel free to submit a PR to change char to letter.

Thinking of those with prior experience with C/Fortran... would it be helpful to check their understanding? Something like:

> ## Identify variable
>
> In the following examples taken out of context, please identify all used variables:
> 
> ~~~
> for char in word:
>   print(char)
> ~~~
> {: .language-python}
> 
> 1. `for`
> 2. `char`
> 3. `in`
> 4. `word`
> 
> > ## Solution
> > ...
> {: .solution}
{: .challenge}
@donnaaboise

This comment has been minimized.

donnaaboise commented Jul 18, 2018

Interesting challenge to ask students. In what context would such a question get asked? In the lesson? Or by the instructor? Or is there a "quiz" at the end of the lesson?

Anyone with some prior language experience should figure out what is going on. But to reduce cognitive load (or just annoyance - I was bothered by this choice the minute I saw it!) , maybe better to use variable names that are not commonly used as type names in other languages. Of course, this can't be followed strictly - there will be inevitable conflicts.

@maxim-belkin

This comment has been minimized.

Collaborator

maxim-belkin commented Jul 18, 2018

In the episode as a challenge.

But to reduce cognitive load (or just annoyance - I was bothered by this choice the minute I saw it!) , maybe better to use variable names that are not commonly used as type names in other languages. Of course, this can't be followed strictly - there will be inevitable conflicts.

Annoyance is not our concern. Cognitive load - is.

I thought you were referring to a possible confusion that it's not char that is a variable but in, which, in this case, could be viewed as a variable of type char (following C analogy for (int i = 0; ...; ...)). Using letter would solve this. However, previously there was a comment that someone thought that for char in <something> is a special construct in Python to loop over characters in a string. To address your and that other concern we could repeat that for loop using a different variable. This way we could make it clear that the actual name of a variable has no effect on the loop.

@donnaaboise

This comment has been minimized.

donnaaboise commented Jul 18, 2018

No, I wasn't worried that someone might think the variable was in. But I did worry that they might wonder why a type was appearing in the loop. And even if they didn't, does everyone know that char is short for character?

I think letter is the best choice. A second example using c could demonstrate that the name of the loop index variable doesn't matter (although I guess Python complains if it is a keyword).

I'll submit a PR.

@maxim-belkin

This comment has been minimized.

Collaborator

maxim-belkin commented Jul 18, 2018

right, keywords can not be used as variable names, but I'm not sure we should include this information early in the lesson, if at all.

I'll submit a PR.

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment