Django Book 翻译
菜单>:
TOC
返回
原文:
.. table:: Table H-3. How QueryDicts Differ from StandardDictionaries. +---------------+-------------------------------------------------------------+ |Method |Differences from Standard dict Implementation | +===============+=============================================================+ |``__getitem__``|Works just like a dictionary. However, if the key has more | | |than one value, ``__getitem__()`` returns the last value. | +---------------+-------------------------------------------------------------+ |``__setitem__``|Sets the given key to ``[value]`` (a Python list whose single| | |element is ``value`` ). Note that this, as other dictionary | | |functions that have side effects, can be called only on a | | |mutable ``QueryDict`` (one that was created via ``copy()`` ).| +---------------+-------------------------------------------------------------+ |``get()`` |If the key has more than one value, ``get()`` returns the | | |last value just like ``__getitem__`` . | +---------------+-------------------------------------------------------------+ |``update()`` |Takes either a ``QueryDict`` or standard dictionary. Unlike | | |the standard dictionarys ``update`` method, this method | | |*appends* to the current dictionary items rather than | | |replacing them: | | | | | |:: | | | | | | | | | >>> q = QueryDict('a=1') | | | >>> q = q.copy() # to make it mutable | | | >>> q.update({'a': '2'}) | | | >>> q.getlist('a') | | | ['1', '2'] | | | >>> q['a'] # returns the last | | | ['2'] | | | | | |.. | | | | | | | +---------------+-------------------------------------------------------------+ |``items()`` |Just like the standard dictionary ``items()`` method, except | | |this uses the same last-value logic as ``__getitem()__`` : | | | | | |:: | | | | | | | | | >>> q = QueryDict('a=1&a=2&a=3') | | | >>> q.items() | | | [('a', '3')] | | | | | |.. | | | | | | | +---------------+-------------------------------------------------------------+ |``values()`` |Just like the standard dictionary ``values()`` method, except| | |this uses the same last-value logic as ``__getitem()__`` . | +---------------+-------------------------------------------------------------+
翻译:
.. table:: 表 H-3. QueryDicts 与标准字典的区别 +---------------+-------------------------------------------------------------+ |方法 |与标准字典实现的不同 | +===============+=============================================================+ |``__getitem__``|与一个字典一样。但是,当一个键有多个值时, | | |``__getitem__()`` 返回最后一个值。 | +---------------+-------------------------------------------------------------+ |``__setitem__``|将所给键的值设为 ``[value]`` (一个只有一个 ``value`` 元素的 | | |Python列表)。 注意,因对其它的字典函数有副作用,故它只能被称| | |为一个可变的 ``QueryDict`` (通过 ``copy()`` 创建)。 | +---------------+-------------------------------------------------------------+ |``get()`` |如果一个键多个值,和 ``__getitem__`` 一样, ``get()`` 返回 | | |最后一个值。 | +---------------+-------------------------------------------------------------+ |``update()`` |参数是一个 ``QueryDict`` 或标准字典。 和标准字典的 | | | ``update`` 不同,这个方法*增加*而不是替换一项内容: | | | | | |:: | | | | | | | | | >>> q = QueryDict('a=1') | | | >>> q = q.copy() # 使其可变 | | | >>> q.update({'a': '2'}) | | | >>> q.getlist('a') | | | ['1', '2'] | | | >>> q['a'] # 返回最后一个值 | | | ['2'] | | | | | |.. | | | | | | | +---------------+-------------------------------------------------------------+ |``items()`` |和标准字典的 ``items()`` 方法一样, 不同的是它和 | | |``__getitem()__`` 一样,返回最后一个值: | | | | | |:: | | | | | | | | | >>> q = QueryDict('a=1&a=2&a=3') | | | >>> q.items() | | | [('a', '3')] | | | | | |.. | | | | | | | +---------------+-------------------------------------------------------------+ |``values()`` |和标准字典的 ``values()`` 方法一样, 不同的是它和 | | |``__getitem()__`` 一样,返回最后一个值。 | +---------------+-------------------------------------------------------------+
备注:
译者: