[Tutor] Python Generator expressions
David L Neil
PyTutor at DancesWithMice.info
Tue Jul 23 19:26:26 EDT 2019
Hi Animesh,
Unfortunately the list server/email has removed the formatting from your
sample, but no matter...
On 24/07/19 5:06 AM, Animesh Bhadra wrote:
> # This code creates a generator and not a tuple comprehensions.
> my_square =(num *num fornum inrange(11))
> print(my_square) # <generator object <genexpr> at 0x7f3c838c0ca8>
> # We can iterate over the square generator like this.
> try:
> whileTrue:
> print(next(my_square)) # Prints the value 0,1,4....
> exceptStopIterationasSI:
> print("Stop Iteration")
> # Another iteration
> forx inmy_square:
> print(x) # This prints nothing.
> Does the generator exhausts its values when we run the iterator once?
Yes, it involves a "lazy" approach - the value is not calculated until
the next() requests it. Whereas, a list comprehension calculates all its
values at one time (and requires the storage space to hold them).
https://docs.python.org/3/reference/datamodel.html?highlight=generator -
notice that there is a yield and a next facility, but when it terminates
StopIteration is raised. There is no 'start again' command!
The Python docs are informative:
https://docs.python.org/3/reference/expressions.html?highlight=generator
https://docs.python.org/3/tutorial/classes.html?highlight=generator#generators
If you have an actual reason for running through a generator expression
more than once, then consider return-ing it from a function/class (which
will then be directly accessible to the for-loop/next method).
> Lastly any specific reason for not having a tuple comprehensions?
> Have checked this link, but could not understood the reason?
> https://stackoverflow.com/questions/16940293/why-is-there-no-tuple-comprehension-in-python
I don't know.
Have you understood the differences between lists and tuples -
specifically "mutability" and "immutability"?
Let's take a list comprehension. If you 'unwind it', can you reproduce
it as a multi-line for-loop? Yes, but before the loop the 'target' list
must be defined/declared to be a list; and within the loop the list is
appended with the 'generated' values.
Ok? (sorry, don't know if this will be new to you, or not)
Now, instead of a list, try using a tuple? How do you append() to a tuple?
Yes, many people have confused generator expressions -
surrounded/"delimited" by parentheses, ie ( and ), with tuples.
However, try this little demonstration:
>>> a, b = 1, 2
>>> a
1
>>> b
2
>>> a, b = ( 1, 2 )
>>> a
1
>>> b
2
>>> ( a, b ) = ( 1, 2 )
>>> a
1
>>> b
2
>>> type( a )
<class 'int'>
>>> type( ( 1, 2 ) )
<class 'tuple'>
The principle here is known as "tuple unpacking". The two constants
(right-hand side) are arranged as a tuple, as are the two variables (a
and b/left-hand side), regardless of the presence/absence of the
parentheses!
https://treyhunner.com/2018/03/tuple-unpacking-improves-python-code-readability/
Clarifying the difference/similarity in appearance between a generator
expression and a tuple, it might help to think that it is the comma(s)
which make it a tuple!
--
Regards =dn
More information about the Tutor
mailing list