Skip to content
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

Lesson Contribution - Swapping variables #883

Closed
klbarnes20 opened this issue Oct 20, 2020 · 4 comments · Fixed by #917
Closed

Lesson Contribution - Swapping variables #883

klbarnes20 opened this issue Oct 20, 2020 · 4 comments · Fixed by #917

Comments

@klbarnes20
Copy link

I'm a member of The Carpentries staff 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.


  1. python-novice-inflammation -

Episode 8 - *https://swcarpentry.github.io/python-novice-inflammation/08-func/index.html
https://swcarpentry.github.io/python-novice-inflammation/08-func/index.html;

  • *Creating functions introduces swapping variables under exercise "The
    Old Switcheroo". I would like to suggest mentioning that there is an
    elegant way of swapping variables in python.
    b, a = a, b
@svaberg
Copy link
Contributor

svaberg commented Mar 16, 2021

I would like to contribute a pull request that closes this issue as part of my instructor training checkout.

@svaberg
Copy link
Contributor

svaberg commented Mar 16, 2021

The original issue (raised by @klbarnes20) is, as I understand it, that swapping variables with the help of a temporary value

temp = a
a = b
b = temp

teaches a non-standard way of swapping variables in Python. The pythonic way of swapping variables is via the

a, b = b, a

idiom as explained in this popular Stack Overflow answer. This idiom in also part of the Additional Exercises but is not introduced in the core lessons.

As I see it, the the core 'gotcha' in this exercise is failing to realise that local variables are created when swap is called. Therefore The Old Switcheroo exercise could be changed as follows:

a = 3
b = 7

def swap(a, b):
    a, b = b, a

swap(a, b)

print(a, b)

The idiomatic version of the swap function would, however, invalidates answer candidate 3-4.

This, and the swap idiom not being part of the core curriculum means that I am
leaning towards simply simply noting in the exercise solution that this idiom exists and point the reader to the Additional Exercises.

@ldko
Copy link
Contributor

ldko commented Mar 16, 2021

This discussion is similar to what was in 460. At that time it was decided not mention a, b = b, a. Do you think we should stick to that and close this issue @maxim-belkin ?

@svaberg
Copy link
Contributor

svaberg commented Mar 16, 2021

How about replacing the Switcheroo exercise with another that does not introduce the switching concept at all? Switching is not exactly a basic concept anyhow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants