In our previous post, we have learned about Generator expressions and how we can create generator functions and print values from it. Here, in this post, we will try to learn about generator expression and generator function both but more specifically generator functions in python.
Also, we will do some quick comparison between both of them and see them in action as well.
Let’s recall things about Generator Expression:
GENERATOR EXPRESSION:
The basic syntax of Generator Expression :
Let’s try to understand it with an example as shown below:
(num ** 2 for num in range(5)) Output- <generator object <genexpr> at 0x00000275F69A1CF0>
If we break the generator expression in the above code to match the syntax, we can write:
- Output Expression = num ** 2
- Iterator = for loop
- Iterable = range(5).
Also, you can add conditionals on generator expressions at two places – first is at output expression and other is at iterables and only the values which satisfy the criteria will be provided otherwise the default value in else expression will be printed (for output expression), for iterables part there will be no else statement provided and only those values which satisfy the condition will be printed). You can check here for more idea about this.
From the output we get, we can say that generator expression doesn’t provide us with the result but it returned a generator object on which we can iterate and print the results To know more about how to apply conditional on generator expression and different ways to print the value from its generator object, check our post here.
Generator Function:
Similar to Generator expression, Generator function produces generator objects when called. Just like a normal or regular python function, the Generator function is defined using the keyword def. It yields a sequence of values instead of returning a single value using the keyword yield.
Let’s see generator function in action with some code as shown below:
def num_seq(n): """Generates value from 0 to n""" i = 0 while i < n: yield i i += 1 result = num_seq(5) print(type(result)) <class 'generator'> for item in result: print(item) Output- 0 1 2 3 4
Comparison between generator expression and generator function :
Below is a table to show a quick comparison:
Generator Expression | Generator Function |
For generator expression, we use parenthesis or round bracket () | For Generator Function, we use def keyword just like regular python function |
Returns a generator object | Returns a generator object |
It can be iterated over | It can be iterated over |
Save memory as it is an object on which we can iterate over to produce the elements | Save memory as it is yields value s and create generator object on which we can iterate over to produce the elements |
Values can be easily printed by using for loop or passing it to list as shown here. | Values can be easily printed by iterating over it using a for loop. |
From the above table, we can easily conclude that both generator expression and generator function have a lot of similarity in working. You can use the function for streaming data.
Basically, you can use both expression and function for large size datasets to:
- load dataset file line by line
- you can easily work with streaming data
- read and process the file until all lines are exhausted
For more detailed information, you can view the official Python 3 Documentation. If you want to learn more like installing python and basic concepts, you can check here.
Leave a Reply