Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import contextlib
- import random
- # classical
- class Choice:
- def __init__(self, choices):
- self.iterable = choices
- def __call__(self):
- return random.choice(self.iterable)
- def __enter__(self):
- return self()
- def __exit__(self, *args):
- pass
- # with the decorator function
- @contextlib.contextmanager
- def choice(iterable):
- yield random.choice(iterable)
- with choice([True, False]) as c:
- print(c)
- with Choice([True, False]) as c:
- print(c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement