Python编码规范:IF中的多行条件
2017-03-08 09:40
417 查看
有时我将IF中的长条件分解成几行。最明显的方法是:
在视觉上不是非常非常有吸引力,因为动作与条件混合。但是,这是使用正确的Python缩进4个空格的自然方式。
目前我使用:
但这不是很漂亮。
能推荐一种替代方法吗?
您不需要在第二个条件行上使用4个空格。也许使用:
另外,不要忘记空格比你想象的更灵活:
这两个都是相当丑陋的。
也许失去括号(Style Guide不鼓励这一点)?
这至少给你一些区别。
甚至:
我想我更喜欢:
这里是Style Guide,这(自2010年)建议使用括号。
转载自:http://codeday.me/bug/20170221/3618.html
if (cond1 == 'val1' and cond2 == 'val2' and cond3 == 'val3' and cond4 == 'val4'): do_something
在视觉上不是非常非常有吸引力,因为动作与条件混合。但是,这是使用正确的Python缩进4个空格的自然方式。
目前我使用:
if ( cond1 == 'val1' and cond2 == 'val2' and cond3 == 'val3' and cond4 == 'val4'): do_something
但这不是很漂亮。
能推荐一种替代方法吗?
您不需要在第二个条件行上使用4个空格。也许使用:
if (cond1 == 'val1' and cond2 == 'val2' and cond3 == 'val3' and cond4 == 'val4'): do_something
另外,不要忘记空格比你想象的更灵活:
if ( cond1 == 'val1' and cond2 == 'val2' and cond3 == 'val3' and cond4 == 'val4' ): do_something if (cond1 == 'val1' and cond2 == 'val2' and cond3 == 'val3' and cond4 == 'val4'): do_something
这两个都是相当丑陋的。
也许失去括号(Style Guide不鼓励这一点)?
if cond1 == 'val1' and cond2 == 'val2' and \ cond3 == 'val3' and cond4 == 'val4': do_something
这至少给你一些区别。
甚至:
if cond1 == 'val1' and cond2 == 'val2' and \ cond3 == 'val3' and \ cond4 == 'val4': do_something
我想我更喜欢:
if cond1 == 'val1' and \ cond2 == 'val2' and \ cond3 == 'val3' and \ cond4 == 'val4': do_something
这里是Style Guide,这(自2010年)建议使用括号。
转载自:http://codeday.me/bug/20170221/3618.html
相关文章推荐
- Python编码规范:IF中的多行条件
- python基础-编码_if条件判断
- Python开发编码规范
- 转帖]Python开发编码规范
- python编码规范
- Python编码规范 -- Python Style Guide
- 【python】编码规范(转载)
- Python编码风格规范
- Python开发编码规范
- Python转载[编码规范]
- Python 编码规范
- python基本编码规范
- Python学习总结(三)——编码规范
- Python编码规范
- Python编码规范
- PHP 编码规范 6.4 if与else语句
- Python编码规范
- Python开发编码规范(转)
- Python 编码规范
- python if 多条件和获取字符串长度方法