GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — learn-python/src/additions/test_generators.py at master · jueshi/learn-python Expand file tree/ Copy pathtest_generators.pyMore file actions 39 lines (29 loc) · 1.4 KB / Copy pathtest_generators.pyFile metadata and controls39 lines (29 loc) · 1.4 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 """Generators. @see: https://www.learnpython.org/en/Generators Generators are used to create iterators, but with a different approach. Generators are simple functions which return an iterable set of items, one at a time, in a special way. When an iteration over a set of item starts using the for statement, the generator is run. Once the generator's function code reaches a "yield" statement, the generator yields its execution back to the for loop, returning a new value from the set. The generator function can generate as many values (possibly infinite) as it wants, yielding each one in its turn. """ import random def lottery(): """Generator function example. Here is a simple example of a generator function which returns random integers. This function decides how to generate the random numbers on its own, and executes the yield statements one at a time, pausing in between to yield execution back to the main for loop. """ # returns first 3 random numbers between 1 and 10 # pylint: disable=unused-variable for i in range(3): yield random.randint(1, 10) # returns a 4th number between 10 and 20 yield random.randint(10, 20) def test_generators(): """Yield statement""" for number_index, random_number in enumerate(lottery()): if number_index < 3: assert 0 <= random_number <= 10 else: assert 10 <= random_number <= 20 |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |