swcarpentry / python-novice-gapminder

Plotting and Programming in Python
http://swcarpentry.github.io/python-novice-gapminder/
Other
163 stars 428 forks source link

Use pop() instead of del in the Lists lesson? #618

Open gabrielesh opened 1 year ago

gabrielesh commented 1 year ago

Hi team, I teach this sequence every term as a free workshop at the library with the assistance of an undergraduate CS major. Thank you for all your hard work to build something that works so well. While I myself have no opinion on using pop() vs del in Python, every single undergraduate whom I've invited teach the Lists lesson has refused to teach del and instead taught pop(). This makes me think that there must be a strong preference in programming for using pop(), and that it would be of use to our students to follow convention. If, however, this convention is particular to the U of Oregon's Computer Science Department, I'm happy to withdraw the suggestion. If everyone agrees that pop() is the way to go, I'm happy to work with my students to make the PR. Gabriele

ldko commented 1 year ago

Note: a change was made in the python-novice-inflammation lesson a few years ago to teach pop instead of del.

alee commented 1 year ago

I think this is a quite reasonable suggestion, especially as the novice-inflammation lesson articulates well, staying consistent with list operations and methods as opposed to introducing a new language construct.

I think a PR would be welcome, thanks for raising the issue! Thoughts from others & @vahtras ?

vahtras commented 1 year ago

Well it depends on the context. These are different use cases, and pop() is for when you actually need to return the deleted value for something else. Just del suffices for deletion. Maybe the computer science students have some issue with syntax that does not look object-oriented? A quick check on may laptop gives

timeit(stmt='foo=[1, 2, 3]; foo.pop(1)') 0.12092131999816047 timeit(stmt='foo=[1, 2, 3]; del foo[1]') 0.06836886899691308

i.e. pop is twice as slow in this case, even including object creation, not that it matters much in practice. I don't have a strong preference for either version (but I do object to students not doing as instructed...). We could teach pop() but then there should be an exercise or example where it makes sense, i.e. which uses the return value.

Olav

On Fri, Nov 4, 2022 at 11:19 PM Allen Lee @.***> wrote:

I think this is a quite reasonable suggestion, especially as the novice-inflammation lesson https://github.com/swcarpentry/python-novice-inflammation/pull/686 articulates well, staying consistent with list operations and methods http://swcarpentry.github.io/python-novice-gapminder/11-lists/index.html#appending-items-to-a-list-lengthens-it as opposed to introducing a new language construct.

I think a PR would be welcome, thanks for raising the issue! Thoughts from others & @vahtras https://github.com/vahtras ?

— Reply to this email directly, view it on GitHub https://github.com/swcarpentry/python-novice-gapminder/issues/618#issuecomment-1304303774, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABLLJBIH5OTYBEOTSEKSZR3WGWDWLANCNFSM6AAAAAARWTEYSA . You are receiving this because you were mentioned.Message ID: @.***>

martinosorb commented 1 year ago

After a bit of googling I seem to understand that pop and del have the same computational complexity, and in fact pop is a wrapper around del. Therefore, we must not make it a question of runtime.

I would tend to agree that pop is more elegant because it's a method of the list class. On the other hand, del works with multiple elements (e.g. del lst[2:5]).

I don't have a strong preference either.

alee commented 1 year ago

I think this StackOverflow post articulates well the differences between the two and I like the answer as well: https://stackoverflow.com/questions/24547223/is-del-or-pop-preferred-when-removing-elements-from-dicts

del makes it clear you are only interested in removing the thing from the list. Use pop when you care about using the thing you are removing from the list (that you are treating as a stack). So... that probably still doesn't bring clarity as to whether we should teach one or the other. Maybe both?? :eyes:

martinosorb commented 1 year ago

Personally, I never use pop.

del should definitely be taught somewhere in the lesson. Is it explained anywhere else? I wouldn't remove it from here unless it is.

If somebody wants to add pop alongside I have no problem with it.

martinosorb commented 1 year ago

I can confirm this is the only place where del is explained. I would welcome adding a bit more explanation about it (e.g. in the earlier episode on variables, explain that you can delete a variable; or simply add a passing mention here in the lists episode, saying that it can be used on any variable).