python-novice-gapminder icon indicating copy to clipboard operation
python-novice-gapminder copied to clipboard

Variables copied by reference not mentioned in Lesson 3

Open dalonsoa opened this issue 6 years ago • 5 comments

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

dalonsoa avatar Feb 13 '19 09:02 dalonsoa

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.

dalonsoa avatar Feb 13 '19 10:02 dalonsoa

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.

ntmoore avatar Feb 13 '19 13:02 ntmoore

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.

dvalters avatar Jul 30 '19 08:07 dvalters

As a new instructor, I would like to take a look at this issue as a checkout task during October 2020.

davidherbert2 avatar Sep 29 '20 14:09 davidherbert2

#499 included changes and text improvements for this issue but stalled on feedback:

https://github.com/swcarpentry/python-novice-gapminder/pull/499#pullrequestreview-512370133

alee avatar Apr 19 '23 00:04 alee