[New Concept Exercise]: Unpacking and Multiple Assignment in Python
See original GitHub issueThis issue describes how to implement the unpacking
and multiple assignment
concept exercise for the Python track.
The related concept documents issue can be found here.
✅ Getting started
If you have not yet created or contributed to a concept exercise, this issue will require some upfront reading to give you the needed background knowledge. Some good example exercises to look at in the repo:
💡Example Exercises💡 (click to expand)
We also recommend completing one or more of the concept exercises (they’re called “learning exercises”) on the website.
Please please read the docs before starting.
Posting PRs without reading these docs will be a lot more frustrating for you during the review cycle, and exhaust Exercism’s maintainers’ time. So, before diving into the implementation, please go through the following documents:
General Contributing Docs:
- Contributing to Exercism | Exercism and GitHub | - Contributor Pull Request Guide
- What are those Weird Task Tags about?
- Exercism Formatting and Style Guide
- Exercism Markdown Specification
- Reputation
Documents on Language Tracks and Concept Exercises
- Building Language Tracks: An Overview
- What are Concept Exercises?
- Concept Exercise Specifications
- Concept Exercise Stories
🎯 Goal
This concept exercise is meant to teach an understanding/use of unpacking
and the *
(splat) and **
(double splat) operators in Python.
💡Learning objectives
- Understand/use
unpacking
through the use of*
and**
prefix operators in various scenarios*
and**
as prefixes … not to be confused with*
(multiply) and**
(exponentiation) as infix, or mathematical operators (consider a link in the links doc or a mention in dig deeper.)what happens in the process of “unpacking” - form, ordering, & iteration(this goes in dig deeper or link docs.)- use in arguments to
functions
- use in argument capture for
functions
(aka passing an arbitrary number of arguments – *args * & **kwargs) use in defining(moved to argument exercise).keyword only arguments
- use in iterable (mainly
tuple
andlist
) unpacking & packing - use in
dict
unpacking & packing
- Understand/use
unpacking
viamultiple assignment
- using
multiple assignment
in place ofindexing
- using
multiple assigment
+*
in place ofslicing
using “list-like” syntax & “tuple-like” syntax- unpacking plus “leftovers” via
*
- using
- Differences between straight
multiple assignment
and*
&**
- Deep unpacking
🤔 Concepts
unpacking
unpacking generalizations
multiple assignment
🚫 Topics that are Out of scope
Concepts & Subjects that are Out of Scope (click to expand)
classes
comprehensions
comprehensions
inlambdas
map()
,filter()
orfunctools.reduce()
in acomprehension
function-arguments
beyond explaining briefly how*
,**
work in function arguments, and how*
works in requiring arguments.functools
beyondfunctools.reduce()
(this will get its own exercise)generators
- using an
assignment expression
or “walrus” operator (:=
) alone or in alambda
↩️ Prerequisites
These are the concepts/concept exercises the student should be familiar with before taking on/learning this concept.
Prereqs (click to expand)
basics
bools
comparisons
dicts
dict-methods
functions
function-arguments
higher-order-functions
functional tools
- Identity methods
is
andis not
iteration
lists
list-methods
numbers
sequences
sets
strings
string-methods
tuples
📚 Resources for Writing and Reference
Resources (click to expand)
- Trey Hunner: Asterisks in Python - what they are and how to use them
- Trey Hunner: Multiple assignment and tuple unpacking improve Python code readability
- Dan Bader: Python Nested Unpacking
- PEP 3132: Extended Iterable Unpacking
- PEP 0448: Additional Unpacking Generalizations
- Python Tutorial: Arbitrary Argument Lists & Python Tutorial: Unpacking Argument Lists
- Python Docs: Expression Lists
- Python Docs: Defining Functions
- built-ins: Python Docs
Exercise Ideas & Stories
Should you need inspiration for an exercise story, you can find a collection here. You can also port an exercise from another track, but please make sure to only to include tasks that actually make sense in Python and that add value for a student. Remove/replace/add tasks as needed to make the concept clear/workable.
📁 Exercise Files to Be Created
File Detail for this Exercise (click to collapse)
|
♾️ Exercise Metadata - Track
For more information on concept exercises and formatting for the Python track config.json
, please see config.json
. The track config.json
file can be found in the root of the Python repo.
You can use the below for the exercise UUID. You can also generate a new one via exercism configlet, uuidgenerator.net, or any other favorite method. The UUID must be a valid V4 UUID.
- Exercise UUID :
a0d00a86-9eaa-487f-9114-e604263ccd96
- concepts should be filled in from the Concepts section in this issue
- prerequisites should be filled in from the Prerequisites section in this issue
🎶 Implementation Notes
-
As a reminder, code in the
.meta/examplar.py
file should only use syntax & concepts introduced in this exercise or one of its prerequisite exercises. We run all ourexamplar.py
files through PyLint, but do not strictly require module docstrings. We do require function docstrings similar to PEP257. See this concept exerciseexemplar.py
for an example. -
Please do not use comprehensions, generator expressions, or other syntax not previously covered either in the introduction to this exercise, or to one of its prerequisites. Please also follow PEP8 guidelines.
-
In General, tests should be written using
unittest.TestCase
and the test file should be named<EXERCISE-NAME>_test.py
.- All asserts should contain a “user friendly” failure message (these will display on the webiste to students, so be as clear as you can).
- We use a
PyTest custom mark
to link test cases to exercise task numbers. - We also use
unittest.subtest
to parameterize test input where/when needed. Here is an example testfile that shows all three of these in action.
-
While we do use PyTest as our test runner and for some implementation tests, please check with a maintainer before using a PyTest-specific test method, fixture, or feature.
-
Our markdown and JSON files are checked against prettier . We recommend setting prettier up locally and running it prior to submitting your PR to avoid any CI errors.
🆘 Next Steps & Getting Help
If you'd like to work on this issue, comment saying "I'd like to work on this"
(there is no real need to wait for a response, just go ahead, we’ll assign you and put a[claimed]
label on the issue).- If you have any questions while implementing, please post the questions as comments in here, or contact one of the maintainers on our Slack channel.
Issue Analytics
- State:
- Created a year ago
- Comments:19 (19 by maintainers)
Top GitHub Comments
You can assign me to both. I will do a quick rewrite until tomorrow so I can show you the exercise in python
Alright, some changes:
Added links (feel free to change them/ add some).
Took in your feedback, I am not sure I covered everything so if you find something just let me know.