0

I came across the term "protocol-driven circuit breaking operator" in PEP 532, and couldn't find what exactly it means.

Can you explain what this phrase means?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Simplicity
  • 47,404
  • 98
  • 256
  • 385

1 Answers1

1

In this context, a "protocol" is an API. And "circuit breaking" might be more easily understood by another phrase, "short circuiting." As in:

if text is None or not text.startswith("lorem"):
    raise RuntimeError("text should exist and start with 'lorem'")

Since text.startswith() is only valid code if text is not None, the above code "short circuits" using the or operator, which does not bother to evaluate the right hand argument if the left-hand is true. Similarly, the and operator short-circuits if the left-hand is false (because the result then must be false).

So the point of this PEP is to enhance the facilities for building expressions which can short-circuit in more elaborate ways.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Thanks John for your nice answer. I think in your statement "Since text.startswith() is only valid code if text is not None", you meant to say "... if text is None" (removing "not")? – Simplicity Dec 06 '16 at 00:45
  • @Simplicity: No, what I wrote is correct. If text is None you can't operate on it. – John Zwinck Dec 06 '16 at 03:17