Description
This issue describes how to implement the generators
concept exercise for the Python track.
Getting started
Please 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 read up on the following documents:
- The features of v3.
- Rationale for v3.
- What are concept exercise and how they are structured?
- Python Track concept exercise implementation guide.
Please also watch the following video:
Goal
The goal of this exercise is to teach the syntax and use of generators
in Python.
Learning objectives
- Understand what generators are and how/when to use them
- Understand how generators relate to
loops
anditerators
- Understand how to use the
yield
keyword - Understand the
__next__()
method - Create a generator
Out of scope
- Memory and performance characteristics and optimizations
throw(type, value=None, traceback=None)
close()
generator expressions
yield from
generators
used as coroutines
Concepts covered
generators
yield
__next__()
iterators
Prerequisites
conditionals
dicts
functions
higher-order-functions
lists
loops
iteration
iterators
sequences
Resources to refer to
- Generators (Python official docs)
- generator (Python official docs glossary)
- The yield statement (Python official docs)
- Yield expressions (Python official docs)
- Iterators(Python official docs)
- Generator-iterator methods (Python official docs)
- How to Use Generators and yield in Python (Real Python)
Hints
- Referring to one or more of the resources linked above, or analogous resources from a trusted source.
Generators
section of the Python Docs Functional How to tutorial: Generators
Concept Description
(a variant of this can be used for the v3/languages/python/concepts/<concept>/about.md
doc and this exercises introduction.md
doc.)
Concept Description Needs to Be Filled In Here/Written
Some "extras" that we might want to include as notes in the concept description, or as links in links.json
:
- Additional
Generator-iterator methods
, such asgenerator.send()
andgenerator.throw()
generator expressions
- Asynchronous generator functions
generators
used as coroutines
Representer
No changes required.
Analyzer
No changes rquired.
Implementing
The general Python track concept exercise implantation guide can be found here.
Tests should be written using unittest.TestCase
and the test file named generators_test.py
.
Code in the .meta/example.py
file should only use syntax & concepts introduced in this exercise or one of its prerequisites. Please do not use comprehensions, generator expressions, or other syntax not previously covered. Please also follow PEP8 guidelines.
Help
If you have any questions while implementing the exercise, please post the questions as comments in this issue, or contact one of the maintainers on our Slack channel.