2013-03-26 39 views
2

有没有办法让这段代码更漂亮?如果在Python 3.3中有一个很好的方法吗?

 
strong = li.find_all("strong") 
if strong: 
    yield li.find_all("strong") 

我的意思是这样的:

 
strong = li.find_all("strong") 
yield li.find_all("strong") if strong 
+3

如何更好地隐藏病情? – 2013-03-26 20:28:09

+2

这两者是否相等取决于'li.find_all'的作用/返回,但通常不会写成'strong = li.find_all(“strong”)'和'strong strong:yield strong'?我的眼中唯一的“无用的”部分是重复(这可能是必要的,我想)。 – DSM 2013-03-26 20:30:16

回答

6

你会使用:的

strong = li.find_all("strong") 
if strong: 
    yield strong 

,而不是调用find_all()再次(在BeautifulSoup,给出了同样的结果,但做的工作再次)。

没有“条件收益率”。你可以玩yield from的技巧,但我建议反对。

+0

哦,哇,我错过了使用同样方法两次的事实。谢谢! – UnstableFractal 2013-03-26 21:22:35

相关问题