Contenido del Curso
Python Functions Tutorial
Python Functions Tutorial
Generator Functions
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. This function is a generator that yields logins one by one from the given list:
def unique_logins_from_list(login_list): # Iterate over each login in the list for login in login_list: yield login # `yield` the current login # A predefined list of available logins login_list = ["user1", "user2", "user3", "user4", "user5"] # Creating a generator instance from the login list login_generator = unique_logins_from_list(login_list) # Generate and print 5 logins, one at a time for _ in range(5): # Each call to `next()` gives the next login print(next(login_generator))
The principle of a generator is that it allows values to be returned one at a time using the yield
keyword, without storing them all in memory at once. In our example, the unique_logins_from_list
generator iterates through the list of logins, returning each one on yield
and pausing at that point. When next()
is called, the generator resumes from where it left off, efficiently yielding values without needing to store the entire list in memory simultaneously. This makes generators particularly useful for handling large data sets or streams of data.
Swipe to show code editor
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.
¡Gracias por tus comentarios!
Generator Functions
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. This function is a generator that yields logins one by one from the given list:
def unique_logins_from_list(login_list): # Iterate over each login in the list for login in login_list: yield login # `yield` the current login # A predefined list of available logins login_list = ["user1", "user2", "user3", "user4", "user5"] # Creating a generator instance from the login list login_generator = unique_logins_from_list(login_list) # Generate and print 5 logins, one at a time for _ in range(5): # Each call to `next()` gives the next login print(next(login_generator))
The principle of a generator is that it allows values to be returned one at a time using the yield
keyword, without storing them all in memory at once. In our example, the unique_logins_from_list
generator iterates through the list of logins, returning each one on yield
and pausing at that point. When next()
is called, the generator resumes from where it left off, efficiently yielding values without needing to store the entire list in memory simultaneously. This makes generators particularly useful for handling large data sets or streams of data.
Swipe to show code editor
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.
¡Gracias por tus comentarios!