Python 中的不等式检查字符的序号

Inequalities in Python to check character's ordinal number

33 <= cp <= 47cp >= 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


P/S:我问这个问题是因为我找不到 Google AI 工程师更喜欢 https://github.com/google-research/bert/blob/master/tokenization.py#L386 上的原始 _is_punctuation 实现的原因

不,它们在语义上是相同的。您还可以 return 条件而不是使用 if 子句,因为它无论如何都会评估为布尔值:

return (33 <= cp <= 47) or (58 <= cp <= 64) or (91 <= cp <= 96) or (123 <= cp <= 126)

他们(Google AI 工程师)可能不知道链式比较,或者他们