Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8056

Re: new string-formatting preferred? (was "What is this syntax ?")

From Terry Reedy <tjreedy@udel.edu>
Subject Re: new string-formatting preferred? (was "What is this syntax ?")
Date 2011-06-20 22:17 -0400
References <mailman.178.1308554725.1164.python-list@python.org> <87pqm8qh7m.fsf@benfinney.id.au> <4DFFE9D5.30300@tim.thechases.com>
Newsgroups comp.lang.python
Message-ID <mailman.209.1308622639.1164.python-list@python.org> (permalink)

Show all headers | View raw


On 6/20/2011 8:46 PM, Tim Chase wrote:
> On 06/20/2011 05:19 PM, Ben Finney wrote:
>> “This method of string formatting is the new standard in
>> Python 3.0, and should be preferred to the % formatting
>> described in String Formatting Operations in new code.”
>>
>> <URL:http://docs.python.org/library/stdtypes.html#str.format>
>
> Is there a good link to a thread-archive on when/why/how .format(...)
> became "preferred to the % formatting"?

That is a controversial statement.

 > I haven't seen any great wins of
> the new formatting over the classic style. Is there some great feature
> of new-style formatting that I've missed out on that obviates bajillions
> of lines of 2.x code?

It does not abuse the '%' operator, it does not make a special case of 
tuples (a source of bugs), and it is more flexible, especially 
indicating objects to be printed. Here is a simple example from my code 
that would be a bit more difficult with %.

multi_warn = '''\
Warning: testing multiple {0}s against an iterator will only test
the first {0} unless the iterator is reiterable; most are not.'''.format
...
print(multiwarn('function'))
...
print(multiwarn('iterator'))

Here is a more complex example:

class chunk():
	def __init__(self, a, b):
		self.a,self.b = a,b
c=chunk(1, (3,'hi'))
print('{0.__class__.__name__} object has attributes int a <{0.a}> and 
tuple b with members <{0.b[0]}> and <{0.b[1]}>'.format(c))
 >>>
chunk object has attributes int a <1> and tuple b with members <3> and <hi>

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: What is this syntax ? Claudiu Popa <cpopa@bitdefender.com> - 2011-06-20 10:18 +0300
  Re: What is this syntax ? Ben Finney <ben+python@benfinney.id.au> - 2011-06-21 08:19 +1000
    Re: new string-formatting preferred? (was "What is this syntax ?") Tim Chase <python.list@tim.thechases.com> - 2011-06-20 19:46 -0500
    Re: new string-formatting preferred? (was "What is this syntax ?") Terry Reedy <tjreedy@udel.edu> - 2011-06-20 22:17 -0400
    Re: new string-formatting preferred? (was "What is this syntax ?") Tim Chase <python.list@tim.thechases.com> - 2011-06-21 06:33 -0500
    Re: new string-formatting preferred? (was "What is this syntax ?") Terry Reedy <tjreedy@udel.edu> - 2011-06-21 18:19 -0400
    Re: new string-formatting preferred? (was "What is this syntax ?") Tim Chase <python.list@tim.thechases.com> - 2011-06-21 18:02 -0500

csiph-web