考虑下面的代码:
if (something1 is not None and
check_property(something_else) and
dr_jekyll is mr_hyde):
do_something(*args)
other_statements()尽管代码是以PEP-8的方式编写的,但显然很难判断谓词的结束位置和主体语句的开始位置。
我们设计了两个变体:
if ((something1 is not None) and
(check_property(something_else)) and
(dr_jekyll is mr_hyde)):
do_something(*args)
other_statements()这是丑陋的
if (something1 is not None and
check_property(something_else) and
dr_jekyll is mr_hyde):
do_something(*args)
other_statements()这也很丑陋。
我个人更喜欢#1,我的同事使用#2。有没有一个非丑陋的和PEP-8兼容的规范解决方案,可以改善上面列出的方法的可读性?
发布于 2013-11-14 23:26:54
使用all()更改if语句
if all([something1 is not None,
check_property(something_else),
dr_jekyll is mr_hyde]):
#do stuff...发布于 2013-11-14 23:32:36
根据您的上下文,您可能不需要is not None
>>> a = [1]
>>> if a:
print "hello, world"
hello, world
>>> if a is not None:
print "hello, world"
hello, world
>>> https://stackoverflow.com/questions/19981513
复制相似问题