+ 7

question to itertools.cycle()

I made an example which you can see here: https://code.sololearn.com/cDdsshO0cKYN/?ref=app Why are the results in both lines of the output not identical? I would have supposed that the expressions 'it', which is set equal to 'count(a)', and 'count(a)' are totally equivalent.

2nd Jun 2018, 9:38 AM
Jan Markus
4 ответов
0
As cycle() function is continuous, it progress until program is stopped. So I guess variable "it" is holding iterable having cycle of a. According to python documentations, cycle Make an "iterator" returning elements from the "iterable" and saving a copy of each. When the iterable is exhausted, return elements from the saved copy. so when u do >>>print (next(cycle (a))) cycle returned an iterable holding value of first element of a so it might be like : >>>print (next(iter [3])) Correct me if I'm wrong.
2nd Jun 2018, 10:33 AM
Yugabdh
Yugabdh - avatar
0
In variant 2 you create a new iterator (which starts from the beginning of the list) in each iteration of the loop, so you get only the same value. A simple substitution of the expression "cycle(a)" is not possible in this case, because "cycle(a)" does something which is called a side effect.
2nd Jun 2018, 9:58 PM
Manuel Maier
Manuel Maier - avatar
0
after looking closely at the code then running it i noticed that you dont have the cycle printing, you defined it as cycle a then attempt to call it with its definition rather than 'it' the cycle defined
6th Jun 2018, 10:07 PM
Ever Drakonis
0
when defining something you can use just about anything however when calling you have to be very specific even one syllable causes issue
6th Jun 2018, 10:33 PM
Ever Drakonis