import random class BingoCage: def __init__(self, items): self._items = list(items) random.shuffle(self._items) def pick(self): try: return self._items.pop() except IndexError: return LookupError(‘pick from empty BingoCage‘) def __call__(self, *args, **kwargs): return self.pick()
>>> bingo = BingoCage(range(3)) >>> bingo.pick() 1 >>> bingo() 0 >>> callable(bingo) True
def tag(name, *content, cls=None, **attrs): """生成一个或多个HTML标签""" if cls is not None: attrs[‘class‘] = cls if attrs: attr_str = ‘‘.join(‘ %s="%s"‘ % (attr, value) for attr, value in sorted(attrs.items())) else: attr_str = ‘‘ if content: return ‘\n‘.join(‘<%s%s>%s</%s>‘ % (name, attr_str, c, name) for c in content) else: return ‘<%s%s />‘ % (name, attr_str)
>>> tag(‘br‘) ? ‘<br />‘ >>> tag(‘p‘, ‘hello‘) ? ‘<p>hello</p>‘ >>> print(tag(‘p‘, ‘hello‘, ‘world‘)) <p>hello</p> <p>world</p> >>> tag(‘p‘, ‘hello‘, id=33) ? ‘<p id="33">hello</p>‘ >>> print(tag(‘p‘, ‘hello‘, ‘world‘, cls=‘sidebar‘)) ? <p class="sidebar">hello</p> <p class="sidebar">world</p> >>> tag(content=‘testing‘, name="img") ? ‘<img content="testing" />‘ >>> my_tag = {‘name‘: ‘img‘, ‘title‘: ‘Sunset Boulevard‘, ... ‘src‘: ‘sunset.jpg‘, ‘cls‘: ‘framed‘} >>> tag(**my_tag) ? ‘<img class="framed" src="sunset.jpg" title="Sunset Boulevard" />‘
>>> def f(a, *, b): ... return a, b ... >>> f(1, b=2) (1, 2)
原文:https://www.cnblogs.com/xiangxiaolin/p/11607750.html