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

Add mention of swap idiom to 'The Old Switcheroo' exercise #917

Merged
merged 8 commits into from
Apr 6, 2021
4 changes: 4 additions & 0 deletions _episodes/08-func.md
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,10 @@ readable code!
> > `a` and `b` in this case) and trades their values. The function does not
> > return any values and does not alter `a` or `b` outside of its local copy.
> > Therefore the original values of `a` and `b` remain unchanged.
> >
> > Note that the recommended way of swapping the content of `a` and `b` in Python is
> > `a, b = b, a`. You can see why this works in the
> > [Additional Exercises]({{ page.root }}/extra_exercises/index.html).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the resolution of the issue #460, I am not sure we want to include this idiom. Also, with the challenge ultimately being about variable scope rather than how to swap variable values, it may be distracting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about this overnight. From a distractions perspective the whole swapping functionality of the function may be distracting from the challenge itself. Maybe the The Old Switcheroo exercise should be replaced by a function that does not switch variables at all.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you about the swapping being distracting; this seems something of a trick question. Looking at the other exercises based on your comment, I notice the challenge titled "Variables Inside and Outside Functions" seems to highlight the same concept of variable scope. Perhaps the Old Switcheroo could go away altogether in that case. Additionally, scanning over the episode material, I am not seeing us actually introduce the concept of variable scope or local variable...if that is the case, perhaps it is something we need to add... @maxim-belkin ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that the Variables Inside and Outside Functions challenge teaches essentially the same content, and in a less cluttered way. As such, maybe The Old Switcheroo should indeed be removed.

Maybe some of the explanatory text from the The Old Switcheroo solution about local variables could be shifted over to the Variables Inside and Outside Functions?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to move some of the description, such as adding to the end of what is there:

When the f2k function is called, it creates a local variable k. The function does not return any values and does not alter k outside of its local copy. Therefore the original value of k remains unchanged.

I think it could also be useful to create a glossary entry for local variable and link to it there. The text could also differ somewhat if you think it could be explained better in a different way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Does this mean we are moving towards removing The Old Switcheroo? I'll try and implement the changes you propose.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless @maxim-belkin or others in the community speaks up about why we should keep the exercise, it would be great for you to carry out the proposed changes. Thank you, @svaberg!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've carried out all the proposed changes but I have not yet removed the Old Switcheroo. I'll do that now in a separate commit.

> {: .solution}
{: .challenge}

Expand Down
5 changes: 3 additions & 2 deletions _extras/extra_exercises.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ or not (yet) added to the main lesson.
> Compare it to:
>
> ~~~
> left, right = [right, left]
> left, right = right, left
maxim-belkin marked this conversation as resolved.
Show resolved Hide resolved
> ~~~
> {: .language-python}
>
Expand All @@ -44,7 +44,8 @@ or not (yet) added to the main lesson.
> >
> > In the first case we used a temporary variable `temp` to keep the value of `left` before we
> > overwrite it with the value of `right`. In the second case, `right` and `left` are packed into a
> > list and then unpacked into `left` and `right`.
> > [tuple]({{ page.root }}/reference.html#tuple)
maxim-belkin marked this conversation as resolved.
Show resolved Hide resolved
> > and then unpacked into `left` and `right`.
> {: .solution}
{: .challenge}

Expand Down