[Python-Dev] Subtle difference between f-strings and str.format()
On Fri, Mar 30, 2018 at 3:29 AM, Serhiy Storchaka <storchaka at gmail.com> wrote:
> 29.03.18 18:06, Terry Reedy ????:
>> On 3/28/2018 11:27 AM, Serhiy Storchaka wrote:
>>> The optimizer already changes semantic. Non-optimized "if a and True:"
>>> would call bool(a) twice, but optimized code calls it only once.
>> Perhaps Ref 3.3.1 object.__bool__ entry, after " should return False or
>> True.", should say something like "Should not have side-effects, as
>> redundant bool calls may be optimized away (bool(bool(ob)) should have the
>> same result as bool(ob))."
> Do you meant that it should be idempotent operation? Because bool(bool(ob))
> always have the same result as bool(ob)) if bool(ob) returns True or False.
And bool(obj) does always return True or False; if you define a
__bool__ method that returns something else then bool rejects it and
raises TypeError. So bool(bool(obj)) is already indistinguishable from
However, the naive implementation of 'if a and True:' doesn't call
bool(bool(a)), it calls bool(a) twice, and this *is* distinguishable
by user code, at least in principle.
If we want to change the language spec, I guess it would be with text
like: "if bool(obj) would be called twice in immediate succession,
with no other code in between, then the interpreter may assume that
both calls would return the same value and elide one of them".
Nathaniel J. Smith -- https://vorpus.org