Week 3- Cont.-Decorator & Generators Flashcards

Decoders (9 cards)

1
Q

What is a decorator?

A

A decorator is a special function that enhances an existing function with additional functionality.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the steps to create a decorator?

A
  1. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a typical use case for decorators related to logging?

A

Decorators are used to log the input, output, or execution time of a function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can decorators be used for caching?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the purpose of using decorators for validation?

A

Decorators can check the validity of the input or output of a function and raise exceptions if necessary.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are generators?

A

Generators provide an iterable series of values and use yield statements to suspend their state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a key benefit of using generators?

A

Generators offer a memory-friendly method of handling large or infinite datasets through lazy evaluation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can generators assist in processing large datasets?

A

Generators can read and process large datasets in chunks without loading the entire dataset into memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a typical use case for generators in implementing iterators?

A

Generators can create custom iterators that produce a sequence of values based on specific patterns or algorithms.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly