Currently both arguments of the boolean operators "and" and "or" are evaluated at compile time. impement short cut semantics because everybody expects this.
Possible solutions:
- Follow Python's implementation closely:
- At compile time call argument.__bool__() and decide in special purpose code.
- For generated code use special function which is converted by code generator back into shortcut
operator. (Similar to current implementation.)
- Implement idea from Greg Ewing (PEP 335: http://www.python.org/dev/peps/pep-0335/):
-Special methods for boolean operators 'and', 'or'
To retain the shortcut semantics split the execution of the operator into two phases:
- first call __and1__(self), __or1__(self) if these operators
can compute the result they return True/False; otherwise they return the special value NeedOtherOperand.
- if NeedOtherOperand was returned call: __and2__(self, other), __rand2__(self, other) __or2__(self, other), __ror2__(self, other)
- Calls with unknown arguments always end up as: __xx2__ (this is implemented)
Currently both arguments of the boolean operators "and" and "or" are evaluated at compile time. impement short cut semantics because everybody expects this.
Possible solutions:
- Follow Python's implementation closely:
- At compile time call argument.__bool__() and decide in special purpose code.
- For generated code use special function which is converted by code generator back into shortcut
operator. (Similar to current implementation.)
- Implement idea from Greg Ewing (PEP 335: http:// www.python. org/dev/ peps/pep- 0335/):
operator into two phases:
return the special value NeedOtherOperand.
__and2_ _(self, other), __rand2__(self, other)
__or2_ _(self, other), __ror2__(self, other)
(this is implemented)
-Special methods for boolean operators 'and', 'or'
To retain the shortcut semantics split the execution of the
- first call __and1__(self), __or1__(self) if these operators
can compute the result they return True/False; otherwise they
- if NeedOtherOperand was returned call:
- Calls with unknown arguments always end up as: __xx2__