25

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 sheer curiosity, why is this? And if they aren't operators, what exactly are they?

6 Answers 6

44

Because they're control flow constructs. Specifically:

  • if the left argument to and evaluates to False, the right argument doesn't get evaluated at all
  • if the left argument to or evaluates to True, the right argument doesn't get evaluated at all

Thus, it is not simply a matter of being reserved words. They don't behave like operators, since operators always evaluate all of their arguments.

You can contrast this with bitwise binary operators which, as the name implies, are operators:

>>> 1 | (1/0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>> 1 or (1/0)
1

As you see, the bitwise OR (|) evaluates both its arguments. The or keyword, however, doesn't evaluate its right argument at all when the left argument evaluates to True; that's why no ZeroDivisionError is raised in the second statement.

4
  • 13
    Note, in C, && and || are called "operators" even though they're short-circuiting (don't evaluate the right-hand side depending on the left-hand side's value). Similarly for some other languages. Commented Jan 7, 2010 at 4:00
  • 2
    Yes, I agree the terminology can be varying. The main point though is that they have fundamentally different characteristics than what Python generally calls operators.
    – Antoine P.
    Commented Jan 7, 2010 at 15:54
  • 3
    This is not the reason. If this were the reason, then operators like in would be in the list. and, or, and all other operators composed of letters aren't in the list of operators because that list is about token classification in Python's lexical structure, and tokens made of letters are all classified as identifiers or keywords, regardless of their semantic role. Commented Oct 19, 2018 at 17:34
  • 3
    In parts of the docs that aren't talking about token structure, such as the operator precedence table, and and or are classified as operators. Commented Oct 19, 2018 at 17:35
5

Python does not currently provide any 'xxx' special methods corresponding to the 'and', 'or' and 'not' boolean operators. In the case of 'and' and 'or', the most likely reason is that these operators have short-circuiting semantics, i.e. the second operand is not evaluated if the result can be determined from the first operand. The usual technique of providing special methods for these operators therefore would not work.

Source: PEP 335

PEP 335 talks about adding the ability to have overloadable operators, and discusses this issue a bit.

1
  • It's not about overloading. in is overloadable, but in isn't in the list. Also, your PEP quote even refers to and and or as operators. Commented Oct 19, 2018 at 17:57
2

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.

1

They're classifying them as keywords earlier in the document.

2
  • 3
    True, but that doesn't really mean anything other than that they can't be used as variable names. Commented Jan 6, 2010 at 23:55
  • The URL the OP linked to has to do with lexical analysis, and lexically, they're keywords, not operators. If the question was phrased as one of syntax, then the down-vote would make sense.
    – Jacob
    Commented Jan 7, 2010 at 0:23
1

They're keywords, because they're reserved identifiers, not special tokens of symbols.

0

They can't be redefined to support type-specific operations, so they don't fall under the scope of the other operators.

2
  • 2
    I suspect your causation is wrong. They can't be redefined to support type-specific operations because they're not operators. Commented Jan 7, 2010 at 0:03
  • in is overloadable, but in isn't on the list. Commented Oct 19, 2018 at 17:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.