osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Python-Dev] Subtle difference between f-strings and str.format()


On 3/30/2018 6:29 AM, Serhiy Storchaka 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.

That is what the parenthetical comment says, but it is not right in the 
context and should be deleted.

For the "if a and True:" example,
'redundant bool calls may be optimized away.'
might be better written as
'duplicate implied __bool__ calls may be avoided.'

What I am trying to say is that *we* define the intended behavior of 
special methods, and we should define what an implementation may 
actually expect.  The current optimizer expects __bool__ to have no side 
effects, at least none that it need respect.

Having said what __bool__ should do, we can also say what it should not 
do to avoid possible surprises -- at least in production code, as 
opposed to 'testing' code like the examples in this thread.

-- 
Terry Jan Reedy