example-code-2e/07-1class-func/bingocall.py

36 lines
552 B
Python
Raw Normal View History

2020-06-11 18:32:05 +02:00
"""
# tag::BINGO_DEMO[]
>>> bingo = BingoCage(range(3))
>>> bingo.pick()
1
>>> bingo()
0
>>> callable(bingo)
True
# end::BINGO_DEMO[]
"""
# tag::BINGO[]
import random
class BingoCage:
def __init__(self, items):
self._items = list(items) # <1>
random.shuffle(self._items) # <2>
def pick(self): # <3>
try:
return self._items.pop()
except IndexError:
raise LookupError('pick from empty BingoCage') # <4>
def __call__(self): # <5>
return self.pick()
# end::BINGO[]