python-novice-gapminder
python-novice-gapminder copied to clipboard
Variables copied by reference not mentioned in Lesson 3
In Lesson 3, it is stated that "Variables only change value when something is assigned to them" in one of the headings. That is not correct when the variables are copied by reference to another variable: When the original variable is changed, the second one changes, too.
This is easily seen with lists:
a = [1, 2, 3]
b = a
a[2] = 4
print(a)
print(b)
The output in both cases is [1, 2, 4] even though we didn't changed variable b
explicitly. This effect can be very confusing to new users, getting unexpected results when they didn't change their variables, and I think it deserves an explanation somewhere within this lesson.
I'm happy to contribute to that part if it seems like a reasonable addition.
Best wishes, Diego
I've seen that copy by reference is mentioned, just as an example, in Lesson 11: Lists, but I think it should also be discussed here on its own or, at least, clarify this statement is not entirely true and that will be revisited in Lesson 11.
I agree that copy vs point to the original is a confusing and subtle point. I wonder if this would be a useful exercise in episode 3 and/or 11.
Agreed - just finished teaching this course and this was one of the issues that came up. The fact that lists have this behaviour (and Pandas dataframes) leads to a confusing realisation when students start using these data structures, having previously being told the contrary case early on. There is a related exercise in Episode 11: "Copying (or Not)", but could be introduced early on.
As a new instructor, I would like to take a look at this issue as a checkout task during October 2020.
#499 included changes and text improvements for this issue but stalled on feedback:
https://github.com/swcarpentry/python-novice-gapminder/pull/499#pullrequestreview-512370133