diff --git a/tech_docs/python/generators_iterators.md b/tech_docs/python/generators_iterators.md new file mode 100644 index 0000000..48f78d5 --- /dev/null +++ b/tech_docs/python/generators_iterators.md @@ -0,0 +1,50 @@ +Certainly! Let's move on to another crucial topic in Python: generators and iterators. This is an important area that can significantly impact code efficiency and memory usage. Here's a set of "stump the chump" questions on generators and iterators, along with their answers and explanations: + +1. Q: What's the difference between `(x for x in range(10))` and `[x for x in range(10)]`? + A: The first creates a generator object, while the second creates a list. + Explanation: The parentheses version is a generator expression, which creates an iterator that generates values on-the-fly. The square brackets version is a list comprehension, which creates and stores all values in memory immediately. Generators are more memory-efficient for large sequences. + +2. Q: How can you tell if an object is iterable? + A: You can use `isinstance(obj, collections.abc.Iterable)` or try `iter(obj)`. + Explanation: The first method checks if the object inherits from the Iterable abstract base class. The second attempts to get an iterator from the object, which will raise a TypeError if the object is not iterable. + +3. Q: What happens when you try to iterate over a generator twice? + A: The second iteration will yield no values. + Explanation: Generators are single-use iterators. Once exhausted, they cannot be reset. To reuse the values, you'd need to recreate the generator or convert it to a reusable sequence like a list. + +4. Q: How do you create an infinite generator? + A: Use a while True loop in a generator function, e.g.: + ```python + def infinite(): + i = 0 + while True: + yield i + i += 1 + ``` + Explanation: This creates a generator that will yield values indefinitely. It's useful for scenarios where you need a potentially unlimited sequence of values, but don't want to store them all in memory. + +5. Q: What's the output of `list(itertools.islice(itertools.count(), 5))`? + A: [0, 1, 2, 3, 4] + Explanation: `itertools.count()` creates an infinite counter. `itertools.islice()` then takes the first 5 elements from this counter. This demonstrates how to work with infinite iterators safely. + +6. Q: How do you implement a custom iterator? + A: Define both `__iter__()` and `__next__()` methods in your class. + Explanation: The `__iter__()` method should return self, and `__next__()` should return the next value or raise StopIteration when exhausted. This allows you to create objects that can be used in for loops and other iteration contexts. + +7. Q: What's a key difference between `map()` and a list comprehension? + A: `map()` returns an iterator, while a list comprehension returns a list. + Explanation: `map()` is lazy and generates values on-demand, which can be more memory-efficient. List comprehensions eagerly create a full list in memory. + +8. Q: How can you create a generator that yields all permutations of a list? + A: Use `itertools.permutations(my_list)`. + Explanation: This creates an iterator yielding tuples of all possible orderings of the input iterable. It's more efficient than generating all permutations at once, especially for large inputs. + +9. Q: What does the `yield from` statement do? + A: It delegates to a sub-generator or iterable. + Explanation: `yield from` allows you to yield all values from another generator or iterable, streamlining the process of creating composite generators. + +10. Q: How do you convert a generator to a list without using `list()`? + A: You can use a list comprehension: `[x for x in my_generator]` + Explanation: This achieves the same result as `list(my_generator)`, but it can be slightly faster in some cases. It's also a good way to illustrate that list comprehensions can work with any iterable, not just ranges or lists. + +These questions cover various aspects of generators and iterators, from basic concepts to more advanced usage. They touch on memory efficiency, lazy evaluation, and the iterator protocol, which are crucial for writing efficient Python code, especially when dealing with large datasets or complex computations. \ No newline at end of file