String formatting style

Michael Aye kmichael.aye at gmail.com
Wed Sep 17 19:31:16 EDT 2014



On Wednesday, September 17, 2014 4:28:11 PM UTC-7, Michael Aye wrote:
>
>
>
> On Wednesday, September 3, 2014 2:57:56 AM UTC-7, Stefan van der Walt 
> wrote:
>>
>> On Wed, Sep 3, 2014 at 1:20 AM, Michael Aye <kmicha... at gmail.com> wrote: 
>> > It's part of the Python tutorial for many years and common standard I'd 
>> say. 
>> > It's basic use might be cumbersome at first, but its power is great. 
>> > 
>> > Things like "My name is {0[name]}".format(dict(name='Fred')) 
>>
>> With great power comes great responsibility!  I find the above very 
>> close to unreadable. 
>>
>> Compare to: 
>>
>> "My name is %s" % name 
>>
>> This one is an unfair comparison as you have removed the dictionary part 
> (for template codings).
> In this simplest case it just can be
>
> "My name is {}".format(name)
>
> and that is arguable better readable, especially considering the cluttered 
> %s" % part in the old syntax.
> In this simplest case I much prefer not to have all these modulo operators 
> flying around.
> When including dictionary access for run-time templates, obviously it 
> get's more complicated.
> How does the old syntax deal with 2 or more dictionaries?
> Here it would just be another number:
> "Our names are {0[name]} and {1[name]}".format(dict(name="Fred"), 
> dict(name="Susan"))
> Obviously, in real code, one would never define the dictionaries inside 
> the format() but they would be
> determined elsewhere in the code, so in reality this would more look like:
>
> "Our names are {0[name]} and {1[name]} and we live at 
> {0[address]} and {1[address]} respectively".format(d0, d1)
>
>
> BTW, these can be named of course, which improves readability, I think:
"Our names are {cust1[name]) and {cust2[name]} and we live at
{cust1[address]} and {cust2[address]} respectively.".format(cust1=d1, 
cust2=d2)

 

>  Michael
>
> or 
>>
>> "My name is %(name)s" % {'name': 'Fred'} 
>>
>> Stéfan 
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-image/attachments/20140917/b4b0d81b/attachment.html>


More information about the scikit-image mailing list