Help understanding this piece of code
The full source code is @ PEP 333. These two lines:
status, response_headers = header开发者_高级运维s_sent[:] = headers_set
.. and ..
headers_set[:] = [status, response_headers]
What am I looking at here? How does [:]
differ from giving nothing at all (just headers_set
)? If someone could provide an explanation, I'd be really glad.
[:] means that you are overwriting the entire list's contents.
>>> a = [1,2,3]
>>> a[:] = [3,4]
>>> a
[3, 4]
>>> a[]
File "<stdin>", line 1
a[]
^
SyntaxError: invalid syntax
And you can use the same syntax to overwrite some index range of the list:
>>> a[2:] = [3,4]
>>> a
[3, 4, 3, 4]
精彩评论