generator
A function that can pause and resume, producing a series of values one at a time with yield, each only when it is requested.
In more detail
Each time the consumer asks for the next value, the generator runs until its next yield and then pauses. Because values are produced on demand, a generator can walk through a huge file or an endless sequence using almost no memory. In Python any function containing yield is a generator, while in JavaScript generators are written function*. Like any iterator, a generator can be consumed only once.
Examples
The course uses a recursive generator to produce every permutation one at a time.def countdown(n):
while n > 0:
yield n
n -= 1Related terms
- iterator — An object that produces the items of a sequence one at a time, on request, instead of all at once.
- permutation — One particular ordering of a set of items; three items have six permutations.
- backtracking — A search technique that builds a solution one choice at a time and undoes the latest choice as soon as it cannot lead to a valid solution.
- Readable stream — A Node.js stream you read data from, such as a file being read, an incoming HTTP request or a socket.
- generator expression — A comprehension written in parentheses, which produces its items one at a time on demand instead of building a whole list.
- lazy evaluation — Postponing work until its result is actually needed, rather than doing it all in advance.
Taught in
- B-Trees: The Structure Behind Every Database Index — Algorithms & Data Structures for Working Engineers
- Backtracking: Search With Pruning — Algorithms & Data Structures for Working Engineers
- Dynamic Programming: Memoisation And Tabulation — Algorithms & Data Structures for Working Engineers
- Writable Streams: Drain Events & highWaterMark — Node.js Internals & Backend Services
- Backpressure Engineering: Handling Mismatched I/O — Node.js Internals & Backend Services
- Comprehensions — Hands-on Python: Complete Video Course & Book
- Sorting, Keys And Lambdas — Hands-on Python: Complete Video Course & Book
- Iterators And Generators — Hands-on Python: Complete Video Course & Book
- Dates, Times And Random — Hands-on Python: Complete Video Course & Book
- Functions, Return Values And Recursion — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Functions And Exceptions Sprint — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- math, random And platform — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Lambdas, map() And filter() — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Generators And Iterators — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- read, readline, errno And bytearray — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Comprehensions, Closures And Files Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Twelve Traps, Mostly Objects — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
References
- Python glossary: generator
- Python reference: yield expressions
- MDN: function*
- Python tutorial: generators