Why aren't “and” and “or” operators in Python?

前端 未结 6 593
陌清茗
陌清茗 2021-02-07 00:32

I wasn\'t aware of this, but apparently the and and or keywords aren\'t operators. They don\'t appear in the list of python operators. Just out of sh

6条回答
  •  遥遥无期
    2021-02-07 01:09

    The list you're looking at is in the section of the docs describing Python's lexical structure: what kinds of tokens Python code is composed of. In terms of the lexical structure, all tokens with the structure of an identifier are classified as identifiers or keywords, regardless of their semantic role. That includes all tokens made of letters.

    and and or appear in the list of keyword tokens rather than the list of operator tokens because they are composed of letters:

    False      await      else       import     pass
    None       break      except     in         raise
    True       class      finally    is         return
    and        continue   for        lambda     try
    as         def        from       nonlocal   while
    assert     del        global     not        with
    async      elif       if         or         yield
    

    If they were spelled && and || instead of and and or, they would have appeared in the list of operator tokens.

    In sections of the docs that aren't talking about the lexical structure, and and or are considered operators. For example, they're listed under the Operator column in the operator precedence table.

提交回复
热议问题