由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 谁知道这个嵌套的Python if 是啥意思?
相关主题
Help: undefined symbolpython读入文件疑问
repast 请进: python debian安装问题[合集] 我也花了一小时读了一下python
问python高手一个问题Python程序员请进
python smtp 587 连不上gmail, socket error?来个PYTHON LIST COMPREHENSION的问题
快要被Python的兼容性弄疯了Python 的设计狗屎不如
python的re怎么有很多莫名奇妙的行为, 我估计是bug吧yield和goroutine啥区别?
why I can not import Tkinter?FP有的,Python都有!
python 3.5 typing hint普及之後 python真要統治世界了。Scripting language的几个问题
相关话题的讨论汇总
话题: content话题: type话题: item话题: 嵌套话题: return
进入Programming版参与讨论
1 (共1页)
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
4
... 是不是CS的工作很容易啊,我也想试试了
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改得难懂又错。
1 (共1页)
进入Programming版参与讨论
相关主题
Scripting language的几个问题快要被Python的兼容性弄疯了
code swarm (video)python的re怎么有很多莫名奇妙的行为, 我估计是bug吧
python 问题why I can not import Tkinter?
[合集] Python/Jython refactoring 还是比较麻烦的python 3.5 typing hint普及之後 python真要統治世界了。
Help: undefined symbolpython读入文件疑问
repast 请进: python debian安装问题[合集] 我也花了一小时读了一下python
问python高手一个问题Python程序员请进
python smtp 587 连不上gmail, socket error?来个PYTHON LIST COMPREHENSION的问题
相关话题的讨论汇总
话题: content话题: type话题: item话题: 嵌套话题: return