我有这段代码(应该是不言自明的;如果没有,请问):
for tr in completed_taskrevs:
found = False
for nr in completion_noterevs:
if tr.description in nr.body:
completion_noterevs.remove(nr)
found = True
break
assert found
Run Code Online (Sandbox Code Playgroud)
我怎样才能让它更加pythonic?
试试这个:
for tr in compleded_taskrevs:
try:
nrs = (nr for nr in completion_noterevs if tr.description in nr.body)
completion_noterevs.remove(nrs.next())
except StopIteration:
raise ValueError('Some error')
Run Code Online (Sandbox Code Playgroud)
编辑:德文是对的.断言不是方法,更好地使用标准异常.
在这里使用assert/ AssertionError可能是错误的,我会说.它对于"调试断言"很有用,也就是说,确保你的代码是理智的.当你获得无效数据时,它作为一种引发错误的方法没有用,出于很多原因,其中最有趣的可能是断言甚至不能保证执行 - 如果你的代码是用任何优化设置编译的,那么不会.哎呀,即使这是一个调试的东西,我仍然会使用raise--它更具可读性,并且无论何时或为何数据错误都会发生.
所以为了使它更"pythonic",我会删除断言并用更好的东西替换它.碰巧,这样一个更好的东西存在,这就是raise声明.此外,我将使用循环的else子句替换笨拙的值set/check ,这在循环耗尽时执行(或者,对于while循环,当条件变为false时).因此,如果中断,则不执行else子句.
for tr in completed_taskrevs:
for nr in completion_noterevs:
if tr.description in nr.body:
completion_noterevs.remove(nr)
break
else:
raise ValueError("description not found"); # or whatever exception would be appropriate
Run Code Online (Sandbox Code Playgroud)
除此之外,我可能不会改变任何东西.
| 归档时间: |
|
| 查看次数: |
292 次 |
| 最近记录: |