import html # available only in Python 3.x
def make_elements(name, value, **attrs):
keyvals = [' %s="%s"' % item for item in attrs.items()]
attr_str = ''.join(keyvals)
element = '<{name}{attrs}>{value}</{name}>'.format(
name = name,
attrs = attr_str,
value = html.escape(value))
return element
make_elements('item', 'Albatross', size='large', quantity=6)
make_elements('p', '<spam>')
>>> d = {'size': 'large', 'quantity': 6}
>>> d.items()
[('quantity', 6), ('size', 'large')]
Python 2.7.3 (default, Feb 27 2014, 19:58:35)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {'size': 'large', 'quantity': 6}
>>> il = d.items()
>>> it = d.iteritems()
>>> vi = d.viewitems()
>>> il
[('quantity', 6), ('size', 'large')]
>>> it
<dictionary-itemiterator object at 0x7fe555159f18>
>>> vi
dict_items([('quantity', 6), ('size', 'large')])
Python 3.4.1 (default, Aug 12 2014, 16:43:01)
[GCC 4.9.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {'size': 'large', 'quantity': 6}
>>> il = d.items()
>>> it = d.iteritems()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'iteritems'
>>> vi = d.viewitems()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'viewitems'
>>> il
dict_items([('size', 'large'), ('quantity', 6)])
>>> for k, v in it: ... print k, v ... quantity 6 size large >>> for k, v in vi: ... print k, v ... quantity 6 size large
>>> it = d.iteritems()
>>> vi = d.viewitems()
>>> d['newkey'] = 'newvalue'
>>> d
{'newkey': 'newvalue', 'quantity': 6, 'size': 'large'}
>>> vi
dict_items([('newkey', 'newvalue'), ('quantity', 6), ('size', 'large')])
>>> it
<dictionary-itemiterator object at 0x7f50ab898f70>
>>> for k, v in vi:
... print k, v
...
newkey newvalue
quantity 6
size large
>>> for k, v in it:
... print k, v
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: dictionary changed size during iteration
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有