关于谷歌的示例代码 [英] About example code from google
问题描述
为什么这样做
uri = urlparse(self.request.uri)
if uri.query:
query = parse_qs(uri.query)
try:
query = query ['query'] [0]
除了KeyError,err:
query =''
而不仅仅是这个
query = self.request.get('query')
?两个代码块之间有什么区别?
有一些差异。最重要的是,如果关键字 query
在uri中多次出现,第一个版本将返回第一个实例,而第二个版本将返回最后一个实例。
此外,在第二个版本中,如果该键不存在,您将获得 None
而不是 ''
潜在地,v1的作者不相信webob会正确解析他的查询字符串。
Why is this done
uri = urlparse(self.request.uri)
if uri.query:
query = parse_qs(uri.query)
try:
query = query['query'][0]
except KeyError, err:
query = ''
And not simply this
query = self.request.get('query')
? What will the difference be between to two code blocks?
There's a few differences. The biggest is if the key query
appears multiple times in the uri, the first version will return the first instance while the second version will return the last instance.
Also, in the second version, if the key doesn't exist you'll get None
instead of ''
Potentially the author of v1 just didn't trust webob to parse his query string properly.
这篇关于关于谷歌的示例代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!