PEP 532 – A circuit breaking protocol and binary operators | peps.python.org
PEP 532 – A circuit breaking protocol and binary operators PEP 532 – A circuit breaking protocol and binary operators Author : Alyssa Coghlan <ncoghlan at gmail.com>, Mark E. Haase <mehaase at gmail.com> Status : Deferred Type : Standards Track Created : 30-Oct-2016 Python-Version : 3.8 Post-History : 05-Nov-2016 Table of Contents PEP Deferral Abstract Relationship with other PEPs PEP 531: Existence checking protocol PEP 505: None-aware operators PEP 335: Overloadable Boolean operators PEP 535: Rich comparison chaining Specification The circuit breaking protocol ( if-else ) Circuit breaking operators (binary if and binary else ) Overloading logical inversion ( not ) Forcing short-circuiting behaviour Circuit breaking identity comparisons ( is and is not ) Truth checking comparisons None-aware operators Rich chained comparisons Other conditional constructs Style guide recommendations Rationale Adding new operators Naming the operator and protocol Using existing keywords Naming the protocol methods Making binary if right-associative Naming the standard circuit breakers Risks and concerns Design Discussion Protocol walk-through Respecting De Morgan’s Laws Arbitrary sentinel objects Implicitly defined circuit breakers in circuit breaking expressions Implementation Acknowledgements References Copyright PEP Deferral Further consideration of this PEP has been deferred until Python 3.8 at the earliest.
Abstract Inspired by PEP 335 , PEP 505 , PEP 531 , and the related discussions, this PEP proposes the definition of a new circuit breaking protocol (using the method names __then__ and __else__ ) that provides a common underlying semantic foundation for: conditional expressions: LHS if COND else RHS logical conjunction: LHS and RHS logical disjunction: LHS or RHS the None-aware operators proposed in PEP 505 the rich comparison chaining model proposed in PEP 535 Taking advantage of the new protocol, it further proposes that the definition of conditional expressions be revised to also permit the use of if and else respectively as right-associative and left-associative general purpose short-circuiting operators: Right-associative short-circuiting: LHS if RHS Left-associative short-circuiting: LHS else RHS In order to make logical inversion ( not EXPR ) consistent with the above changes, it also proposes the introduction of a new logical inversion protocol (using the method name __not__ ).
To force short-circuiting of a circuit breaker without having to evaluate the expression creating it twice, a new operator.short_circuit(obj) helper function will be added to the operator module. Finally, a new standard types.CircuitBreaker type is proposed to decouple an object’s truth value (as used to determine control flow) from the value it returns from short-circuited circuit breaking expressions, with the following factory functions added to the operator module to represent particularly common switching idioms: switching on bool(obj) : operator.true(obj) switching on not bool(obj) : operator.false(obj) switching on obj is value : operator.is_sentinel(obj, value) switching on obj is not value : operator.is_not_sentinel(obj, value) Relationship with other PEPs This PEP builds on an extended history of work in other proposals.
Some of the key proposals are discussed below. PEP 531: Existence checking protocol This PEP is a direct successor to PEP 531 , replacing the existence checking protocol and the new ?then and ?else syntactic operators defined there with the new circuit breaking protocol and adjustments to conditional expressions and the not operator. PEP 505: None-aware operators This PEP complements the None-aware operator proposals in PEP 505 , by offering an underlying protocol-driven semantic framework that explains their short-circuiting behaviour as highly optimised syntactic sugar for particular uses of conditional expressions.
Given the changes proposed by this PEP: LHS ?? RHS would roughly be is_not_sentinel(LHS, None) else RHS EXPR?.attr would roughly be EXPR.attr if is_not_sentinel(EXPR, None) EXPR?[key] would roughly be EXPR[key] if is_not_sentinel(EXPR, None) In all three cases, the dedicated syntactic form would be optimised to avoid actually creating the circuit breaker instance and instead implement the underlying control flow directly.
In the latter two cases, the syntactic form would also avoid evaluating EXPR twice. This means that while the None-aware operators would remain highly specialised and specific to None, other sentinel values would still be usable through the more general protocol-driven proposal in this PEP. PEP 335: Overloadable Boolean operators PEP 335 proposed the ability to overload the short-circuiting and and or operators directly, with the ability to overload the semantics of comparison chaining being one of the consequences of that change.
The proposal in an earlier version of this PEP to instead handle the element-wise comparison use case by changing the semantic definition of comparison chaining is drawn directly from Guido’s rejection of PEP 335 [1] . However, initial feedback on this PEP indicated that the number of different proposals that it covered made it difficult to read, so that part of the proposal has been separated out as PEP 535 . PEP 535: Rich comparison chaining As noted above, PEP 535 is a proposal to build on the circuit breaking protocol defined in this PEP in order to expand the rich comparison support introduced in PEP 207 to also handle comparison chaining operations like LEFT_BOUND < VALUE < RIGHT_BOUND .
Specification The circuit breaking protocol ( if-else ) Conditional expressions ( LHS if COND else RHS ) are currently interpreted as an expression level equivalent to: if COND : _expr_result = LHS else : _expr_result = RHS This PEP proposes changing that expansion to allow the checked condition to implement a new “circuit breaking” protocol that allows it to see, and potentially alter, the result of either or both branches of the expression: _cb = COND _type_cb = type ( cb ) if _cb : _expr_result = LHS if hasattr ( _type_cb , "__then__" ): _expr_result = _type_cb . __then__ ( _cb , _expr_result ) else : _expr_result = RHS if hasattr ( _type_cb , "__else__" ): _expr_result = _type_cb . __else__ ( _cb , _expr_result ) As shown, interpreter implementations would be required to access only the protocol method needed for the branch of the conditional expression that is actually executed.
Consistent with other protocol methods, the special methods would be looked up via the circuit breaker’s type, rather than directly on the instance. Circuit breaking operators (binary if and binary else ) The proposed name of the protocol doesn’t come from the proposed changes to the semantics of conditional expressions. Rather, it comes from the proposed addition of if and else as general purpose protocol driven short-circuiting operators to complement the existing True and False based short-circuiting operators ( or and and , respectively) as well as the None based short-circuiting operator proposed in PEP 505 ( ?? ).
Together, these two operators would be known as the circuit breaking operators. In order to support this usage, the definition of conditional expressions in the language grammar would be updated to make both the if clause and the else clause optional: test : else_test [ 'if' or_test [ 'else' test ]] | lambdef else_test : or_test [ 'else' test ] Note that we would need to avoid the apparent simplification to else_test ('if' else_test)* in order to make it easier for compiler implementations to correctly preserve the semantics of normal conditional expressions.
The definition of the test_nocond node in the grammar (which deliberately excludes conditional expressions) would remain unchanged, so the circuit breaking operators would require parentheses when used in the if clause of comprehensions and generator expressions just as conditional expressions themselves do. This grammar definition mea
The rest of this page is on the live site.