


Harnessing Generators: A Practical Guide to `yield` and `yield from`
Jul 26, 2025 am 09:43 AMUse yield to create memory-friendly, lazy evaluation generators suitable for handling large files, infinite sequences, and data pipelines; 2. yield from simplifies delegating to another generator, reduces redundant code and improves readability, suitable for recursive traversals (such as tree structures) and generator combinations; 3. The generator is used in a single time and should not be mixed with return. It is recommended to combine itertools for advanced control, ultimately achieving efficient and elegant data stream processing.
Python's yield
and yield from
are powerful tools for writing efficient, readable, and memory-friendly code—especially when working with large or infinite sequences. They unlock the ability to create generators , a type of iterator that produces items on demand, rather than storing everything in memory upfront.
Let's break down how to use them effectively in real-world scenarios.
What Is a Generator and Why Use yield
?
A generator is a function that returns an iterator, producing a sequence of values one at a time using yield
instead of return
. When yield
is hit, the function pauses, saves its state, and resumes from where it left off on the next iteration.
def count_up_to(max): count = 1 While count <= max: yield count count = 1 # Usage for num in count_up_to(5): print(num)
This prints:
1 2 3 4 5
Why this matters:
- Memory efficient: Only one value exists in memory at a time.
- Lazy evaluation: Values are generated only when needed.
- Clean syntax: Looks like a regular function, but behaves like an iterator.
Use
yield
when you're generating a sequence that's expensive to compute or potentially infinite—like reading large files, streaming data, or mathematical sequences.
Practical Use Cases for yield
1. Processing Large Files
Reading a huge log file all at once can exhaust memory. Instead, yield lines one by one:
def read_large_file(file_path): with open(file_path, 'r') as file: for line in file: yield line.strip()
Now you can process millions of lines without loading them all.
2. Infinite Sequences
Generate Fibonacci numbers forever (or until you stop iterating):
def fibonacci(): a, b = 0, 1 While True: yield a a, b = b, ab # First 10 Fibonacci numbers fib = fibonacci() for _ in range(10): print(next(fib))
3. Data Pipelines
Chain generators to build efficient processing pipelines:
def numbers(): for i in range(100): yield i def even_only(nums): for n in nums: if n % 2 == 0: yield n def squared(nums): for n in nums: yield n ** 2 # Chain them result = squared(even_only(numbers())) for val in result: print(val)
Each step processes data lazily—no intermediate lists created.
Simplifying Delegation with yield from
Before Python 3.3, if you wanted to yield all values from another generator inside a generator, you had to loop manually:
def wrapper_old_style(): for value in count_up_to(3): yield value for value in fibonacci(): yield value
Now, yield from
does this cleanly:
def wrapper(): yield from count_up_to(3) yield from fibonacci()
It delegates to another iterable or generator, yielding each value in turn.
Key Benefits of yield from
:
- Reduces boilerplate code.
- Preserves the generator protocol (eg, handles
.send()
,.throw()
,.close()
). - Makes nested generators more readable and maintainedable.
Real-World Example: Tree Traversal
Suppose you're traversing a binary tree and want to yield all values in order:
class Node: def __init__(self, value, left=None, right=None): self.value = value self.left = left self.right = right def inorder(self): if self.left: yield from self.left.inorder() # Delegate to left subtree yield self.value if self.right: yield from self.right.inorder() # Delegate to right subtree
This keeps the recursive structure clean and memory-efficient.
Common Pitfalls and Tips
Don't mix
return
andyield
carefully : In Python versions before 3.3,return
in a generator couldn't have a value. Now,return value
sets theStopIteration.value
, but it's rarely needed.Generators are single-use : Once exhausted, they don't reset. If you need to reuse, wrap the generator in a class or re-call the function.
Use
itertools
with generators : Combine with tools likeislice
,chain
, ortee
for advanced control.
Example: Get first 5 even Fibonacci numbers
from itertools import islice fib_evens = (n for n in fibonacci() if n % 2 == 0) for num in islice(fib_evens, 5): print(num)
Final Thoughts
yield
turns functions into powerful, lazy data producers. yield from
making composing them effortless. Together, they help you write code that's not only efficient but also elegant and easy to reason about.
Use them when:
- You're dealing with large or infinite data.
- You want to decouple data generation from consumption.
- You're building data pipelines or recursive traversals.
They might feel unusual at first, but once you get used to thinking in streams, you'll find yourself reaching for generators more often than lists.
Basically, if you're building something that “produces a bunch of things,” ask: Should this be a generator? More often than not, the answer is yes.
The above is the detailed content of Harnessing Generators: A Practical Guide to `yield` and `yield from`. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PHP8attributesreplaceDocBlocksformetadatabyprovidingtype-safe,nativelysupportedannotations.1.Attributesaredefinedusing#[Attribute]andcantargetclasses,methods,properties,etc.2.Theyenablecompile-timevalidation,IDEsupport,andbetterperformancebyeliminati

Yes,PHPsyntaxiseasy,especiallyforbeginners,becauseitisapproachable,integrateswellwithHTML,andrequiresminimalsetup.Itssyntaxisstraightforward,allowingdirectembeddingintoHTMLwithtags,using$forvariables,semicolonsforstatements,andfamiliarC-stylestructur

PHP's array deconstruction and expansion operators can improve code readability and flexibility through concise syntax. 1. Array deconstruction supports extracting values from indexes and associative arrays, such as [$first,$second]=$colors, which can be assigned separately; elements can be skipped through empty placeholders, such as [,,$third]=$colors; associative array deconstruction requires the => matching key, such as ['name'=>$name]=$user, which supports renaming variables and setting default values to deal with missing keys. 2. Expand operator (...) can expand and merge arrays, such as [...$colors,'blue'], which supports majority combination and associative array overwrite, but subsequent keys will overwrite the former and do not replenish.

PHP8.0'snamedargumentsandconstructorpropertypromotionimprovecodeclarityandreduceboilerplate:1.Namedargumentsletyoupassparametersbyname,enhancingreadabilityandallowingflexibleorder;2.Constructorpropertypromotionautomaticallycreatesandassignsproperties

When a static method is called using self in inheritance, it always points to the class that defines the method, rather than the actually called class, resulting in the inability to call the subclass overridden method as expected; while static uses late static binding, which can correctly parse to the actually called class at runtime. 1. Self is an early binding, pointing to the class where the code is located; 2. static is a late binding, pointing to the runtime calling class; 3. Use static to implement static factory methods and automatically return subclass instances; 4. static supports correct resolution of inherited attributes in the method chain; 5. LSB is only suitable for static methods and attributes, not for constants; 6. Static should be used first in inheritable classes to improve flexibility and scalability, which is in modern PH

PHP's variable functions and parameter unpacking is implemented through the splat operator (...). 1. Variable functions use...$params to collect multiple parameters as arrays, which must be at the end of the parameter list and can coexist with the required parameters; 2. Parameter unpacking uses...$array to expand the array into independent parameters and pass it into the function, suitable for numerical index arrays; 3. The two can be used in combination, such as passing parameters in the wrapper function; 4. PHP8 supports matching named parameters when unpacking associative arrays, and it is necessary to ensure that the key name is consistent with the parameter name; 5. Pay attention to avoid using unpacking for non-traversable data, prevent fatal errors, and pay attention to the limit of parameter quantity. These features improve code flexibility and readability, reducing func_get_args() and so on

Arrow functions are suitable for scenarios with single expressions, simple callbacks and improved readability; 2. Anonymous functions are suitable for scenarios with multi-line logic, complex control flow, referencing external variables and using yield generators; therefore, you should choose according to specific needs: simple scenarios prioritize arrow functions to improve code simplicity, while complex scenarios use anonymous functions to obtain complete functional support.

Theternaryoperator(?:)isusedforsimpleif-elselogic,returningoneoftwovaluesbasedonacondition;2.Thenullcoalescingoperator(??)returnstheleftoperandifitisnotnullorundefined,otherwisetherightoperand,makingitidealforsettingdefaultswithoutbeingaffectedbyfals
