2013-03-26 42 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

相關問題