[PATCH] fix core library string formatting for python 2.6

Douglas Bagnall douglas.bagnall at catalyst.net.nz
Fri Sep 21 04:17:13 UTC 2018


On 21/09/18 15:49, Amitay Isaacs wrote:
>> Please review with a bit of care: *one* way in which Python's .format()
>> is inferior to its % printf formatting is there is no error for
>> unconsumed arguments. That is, if I went:
>>
>> -    "{} {} {}".format(x, y, z)
>> +    "{0} {1} {1}".format(x, y, z)
>>
>> there would be no error to say I had ignored the third argument.
>> So you need to review my ability to count.
> 
> str.format() also accepts field names.  It might be overkill for
> single argument format strings.  But for multiple arguments, it's
> self-documenting if used with sensible field names.

Yes, there are ways in which it is superior too. I like having both. The
worst thing about .format() is nobody remembers the clever things it can
do because it is its own special language. Whereas the printf strings
are printf strings.

> 
> "{0} {1} {2}"  ---> "{first-name} {middle-name} {last-name}"

you'd usually need "{first_name} {middle_name} {last_name}" because
you'd use .format(first_name=X, middle_name=Y,...) where a hyphen would
be read as minus.

> 
> And you also get argument checking as you need to pass the values by name.
> 
> Amitay.
> 

cheers,
Douglas



More information about the samba-technical mailing list