t**********s 发帖数: 930 | 1 if [item for item in httpobj.headers.keys() if "content-type" in item]:
return httpobj.headers['content-type']
被这个嵌套的if 搞糊涂了。 |
d******e 发帖数: 2265 | 2 第二个是list comprihenshion 内部的 if
当然这个代码写的不好读
【在 t**********s 的大作中提到】 : if [item for item in httpobj.headers.keys() if "content-type" in item]: : return httpobj.headers['content-type'] : 被这个嵌套的if 搞糊涂了。
|
d******e 发帖数: 2265 | 3 it equals to
if 'content~type' in httpobj. heasers:
return blah
【在 t**********s 的大作中提到】 : if [item for item in httpobj.headers.keys() if "content-type" in item]: : return httpobj.headers['content-type'] : 被这个嵌套的if 搞糊涂了。
|
n******7 发帖数: 12463 | |
t**********s 发帖数: 930 | 5 能说的更清楚些吗?
原来的Code 是这样的:
if "content-type" in httpobj.headers.keys():
return httpobj.headers['content-type']
不知啥原因改成现在这样了 。
【在 d******e 的大作中提到】 : 第二个是list comprihenshion 内部的 if : 当然这个代码写的不好读
|
d******e 发帖数: 2265 | 6 没法再清楚了。你需要读一下list comprehension的文档
【在 t**********s 的大作中提到】 : 能说的更清楚些吗? : 原来的Code 是这样的: : if "content-type" in httpobj.headers.keys(): : return httpobj.headers['content-type'] : 不知啥原因改成现在这样了 。
|
e***i 发帖数: 231 | 7 第二个相当于filter
【在 t**********s 的大作中提到】 : if [item for item in httpobj.headers.keys() if "content-type" in item]: : return httpobj.headers['content-type'] : 被这个嵌套的if 搞糊涂了。
|
d********g 发帖数: 10550 | 8 这都啥人写的垃圾代码……明明第一个是有bug会出错的
测试:
>>> a = {'content-type': 'text/plain'}
>>> b = {'content-type-typo': 'typo'}
第一个:
>>> if [item for item in a.keys() if "content-type" in item]:
... a['content-type']
...
'text/plain'
>>> if [item for item in b.keys() if "content-type" in item]:
... b['content-type']
...
Traceback (most recent call last):
File "", line 2, in
KeyError: 'content-type'
第二个:
>>> if "content-type" in a:
... a['content-type']
...
'text/plain'
>>> if "content-type" in b:
... b['content-type']
...
实在看不下去了,这种质量的code也能过code review和unittest?
Pythonic的写法很简单,下面几种都行,看具体情况:
1. return httpobj.headers.get('content-type')
2. return httpobj.headers.get('content-type', )
3. return httpobj.headers.get('content-type') or
key-or-value-is-considered-False>
后两种的不同在于处理比如c = {'content-type': ''}这类情况
这些号称会写Python的啊,too young, too simple,sometimes naive :)
【在 t**********s 的大作中提到】 : 能说的更清楚些吗? : 原来的Code 是这样的: : if "content-type" in httpobj.headers.keys(): : return httpobj.headers['content-type'] : 不知啥原因改成现在这样了 。
|
a9 发帖数: 21638 | 9 这显然是某些相显摆的人写的代码。
另外,这个还得注意大小写的问题吧。
key>)
【在 d********g 的大作中提到】 : 这都啥人写的垃圾代码……明明第一个是有bug会出错的 : 测试: : >>> a = {'content-type': 'text/plain'} : >>> b = {'content-type-typo': 'typo'} : 第一个: : >>> if [item for item in a.keys() if "content-type" in item]: : ... a['content-type'] : ... : 'text/plain' : >>> if [item for item in b.keys() if "content-type" in item]:
|
c*********e 发帖数: 16335 | 10 python和cobol很象,适合初级人员写程序。
【在 d********g 的大作中提到】 : 这都啥人写的垃圾代码……明明第一个是有bug会出错的 : 测试: : >>> a = {'content-type': 'text/plain'} : >>> b = {'content-type-typo': 'typo'} : 第一个: : >>> if [item for item in a.keys() if "content-type" in item]: : ... a['content-type'] : ... : 'text/plain' : >>> if [item for item in b.keys() if "content-type" in item]:
|
n******7 发帖数: 12463 | 11 第一个in 换成 == 就可以了吧
不过这么写难读又容易错,估计是coder刚学了list comprehension,要练手
【在 d********g 的大作中提到】 : 这都啥人写的垃圾代码……明明第一个是有bug会出错的 : 测试: : >>> a = {'content-type': 'text/plain'} : >>> b = {'content-type-typo': 'typo'} : 第一个: : >>> if [item for item in a.keys() if "content-type" in item]: : ... a['content-type'] : ... : 'text/plain' : >>> if [item for item in b.keys() if "content-type" in item]:
|
v*******e 发帖数: 11604 | 12 大家说得很对,特别是发现楼主贴的第一个code中的bug的。。。我来帮助一下楼主。
if [item for item in httpobj.headers.keys() if "content-type" in item]:
return httpobj.headers['content-type']
[item for item in httpobj.headers.keys() if "content-type" in item]是一个
list comprehension,它的结果是一个list,对于httpobj.headers.keys()这个list里
面的每一个item,考察"content-type"这个字符串是不是这个item的一部分。如果是,
就放在结果list里;如果不是,就丢弃。剩下的code就是:
if 刚才那个结果list:
return httpobj.headers['content-type']
if考察的是这个结果list是不是空的,如果不是,那么就返回值。如同8楼说的,这个
code是有bug的。把“in”改为“==”这个code就对了,但是确实是一个练手的初哥写
的code,把一个好端端的code改得难懂又错。 |