0

The following is valid Python (3.7.4) code:

def f():
  return 1.and(0)

dis.dis(f) gives

  2           0 LOAD_CONST               1 (1.0)
              2 JUMP_IF_FALSE_OR_POP     6
              4 LOAD_CONST               2 (0)
        >>    6 RETURN_VALUE

This also works for or. Not and xor give SyntaxError. Generally, the structure for this expression seems to be Integer.and(something), where something can be any type.

My interpretation of this is, that this is interpreted as a float with a logic operation as its decimal places.

[EDIT]

a = 3
a.not(1)

yields a SyntaxError

l = [1, 2, 3]
3.in(l)

yields True - I can see the purpose of this.
[/EDIT]

The question: Why does this construct exist and what is its purpose (if any)?

1 Answers1

1

It’s spaced to look like a method call, but it’s actually a float literal, a binary operator, and redundant parentheses:

1.0 and 0

(related: How do "and" and "or" act with non-boolean values?)

Float literals can end with a decimal point, and keywords don’t have to be separated by spaces as long as they can’t be mistaken for anything else.

>>> 1.
1.0
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • OK, this makes sense from the syntax perspective. I usually define my floats this way and stumbled across the above construct when VSCode suggested things to put behind the dot... – iamnotgoodwithcomputer Jul 08 '20 at 08:00