Week 3- Cont.-Decorator & Generators Flashcards
Decoders (9 cards)
What is a decorator?
A decorator is a special function that enhances an existing function with additional functionality.
What are the steps to create a decorator?
- Decorator takes a function as an argument. 2. Decorator body should have an inner function. 3. Decorator should return a function. 4. The extra functionality can be added in the body of the inner function.
What is a typical use case for decorators related to logging?
Decorators are used to log the input, output, or execution time of a function.
How can decorators be used for caching?
Decorators can cache the results of a function that is expensive to compute by saving the output in a dictionary and returning it for the same input.
What is the purpose of using decorators for validation?
Decorators can check the validity of the input or output of a function and raise exceptions if necessary.
What are generators?
Generators provide an iterable series of values and use yield statements to suspend their state.
What is a key benefit of using generators?
Generators offer a memory-friendly method of handling large or infinite datasets through lazy evaluation.
How can generators assist in processing large datasets?
Generators can read and process large datasets in chunks without loading the entire dataset into memory.
What is a typical use case for generators in implementing iterators?
Generators can create custom iterators that produce a sequence of values based on specific patterns or algorithms.