33 <= cp <= 47寫類似vs的東西有區別cp >= 33 and cp <= 47嗎?更具體地說,如果有一個函數可以:def _is_punctuation(char): """Checks whether `chars` is a punctuation character.""" cp = ord(char) if ((cp >= 33 and cp <= 47) or (cp >= 58 and cp <= 64) or (cp >= 91 and cp <= 96) or (cp >= 123 and cp <= 126)): return True else: return False是否與以下內容相同:def is_punctuation(char): """Checks whether `chars` is a punctuation character.""" # Treat all non-letter/number ASCII as punctuation. # Characters such as "^", "$", and "`" are not in the Unicode # punctuation class but treat them as punctuation anyways, for consistency. cp = ord(char) if (33 <= cp <= 47) or (58 <= cp <= 64) or (91 <= cp <= 96) or (123 <= cp <= 126): return True return False是否有理由更喜歡_is_punctuation()或is_punctuation()反之亦然?一個在計算上會比另一個更快嗎?如果是這樣,我們如何驗證呢?使用dis.dis?
1 回答

嗶嗶one
TA貢獻1854條經驗 獲得超8個贊
不,它們在語義上是相同的。您也可以返回條件而不是使用 if 子句,因為它無論如何都會評估為布爾值:
return (33 <= cp <= 47) or (58 <= cp <= 64) or (91 <= cp <= 96) or (123 <= cp <= 126)
他們(谷歌 AI 工程師)可能不知道鏈式比較,或者他們希望它表現得更好一些。
添加回答
舉報
0/150
提交
取消