Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Generator Functions | Function Return Value Specification
Python Functions Tutorial

Generator FunctionsGenerator Functions

In Python, a generator function is a special type of function that uses the yield keyword instead of return to generate a sequence of values. When a generator function is called, it returns an iterator object, which can be iterated over to retrieve values one at a time.
The main advantage of generator functions is their memory efficiency. Generator functions generate values on-the-fly as they are needed, rather than generating the entire sequence upfront. This makes them memory efficient, especially when dealing with large datasets or infinite sequences.

Let's look at the example of the simple generator:

Let's break down each step in more detail:

  • Defining the cat name generator;

In this step, we create a function cat_name_generator which is a generator. The generator uses the keyword yield to return values one by one. In our case, the generator yields cat names from the cat_names list.

  • Creating an instance of the generator;

Here, we create an instance of the generator by calling the cat_name_generator() function. Now, cat_name_gen is a generator object that can be used to retrieve cat names.

  • Calling next() to get the first name;

We call the next() function to retrieve the first value from the generator. The generator runs up to the first yield statement, returns the value ("Whiskers"), and pauses. This value is assigned to the variable first_cat_name, and we print it using print().

After executing these steps, we get the following output:

This way, we obtained the first cat name using the cat name generator and the next() function.

  • Calling next() to get the second name;

Now, we call next() again to retrieve the next value from the generator. The generator resumes from where it left off (after the first yield). If there are no more values to return, next() returns the error. After executing these steps, we get the following output:

Therefore, we obtained the second cat name "Fluffy".

Завдання

Create a generator that generates unique identifiers. The identifiers can be, for example, strings in the format "ID_1", "ID_2", and so on. Each time you call next(), the generator should return a new unique identifier.

Все було зрозуміло?

Секція 4. Розділ 4
toggle bottom row
course content

Зміст курсу

Python Functions Tutorial

Generator FunctionsGenerator Functions

In Python, a generator function is a special type of function that uses the yield keyword instead of return to generate a sequence of values. When a generator function is called, it returns an iterator object, which can be iterated over to retrieve values one at a time.
The main advantage of generator functions is their memory efficiency. Generator functions generate values on-the-fly as they are needed, rather than generating the entire sequence upfront. This makes them memory efficient, especially when dealing with large datasets or infinite sequences.

Let's look at the example of the simple generator:

Let's break down each step in more detail:

  • Defining the cat name generator;

In this step, we create a function cat_name_generator which is a generator. The generator uses the keyword yield to return values one by one. In our case, the generator yields cat names from the cat_names list.

  • Creating an instance of the generator;

Here, we create an instance of the generator by calling the cat_name_generator() function. Now, cat_name_gen is a generator object that can be used to retrieve cat names.

  • Calling next() to get the first name;

We call the next() function to retrieve the first value from the generator. The generator runs up to the first yield statement, returns the value ("Whiskers"), and pauses. This value is assigned to the variable first_cat_name, and we print it using print().

After executing these steps, we get the following output:

This way, we obtained the first cat name using the cat name generator and the next() function.

  • Calling next() to get the second name;

Now, we call next() again to retrieve the next value from the generator. The generator resumes from where it left off (after the first yield). If there are no more values to return, next() returns the error. After executing these steps, we get the following output:

Therefore, we obtained the second cat name "Fluffy".

Завдання

Create a generator that generates unique identifiers. The identifiers can be, for example, strings in the format "ID_1", "ID_2", and so on. Each time you call next(), the generator should return a new unique identifier.

Все було зрозуміло?

Секція 4. Розділ 4
toggle bottom row
some-alt