Pre-PEP: Refusing to guess in string formatting operations

Jp Calderone exarkun at intarweb.us
Thu Mar 13 03:36:06 CET 2003


On Thu, Mar 13, 2003 at 01:58:45PM +1300, Greg Ewing (using news.cis.dfn.de) wrote:
> Beni Cherniavsky wrote:
> >The `string formatting operation`_ - ``format % values`` - has a wart.
> >In the face of non-tuple values, it assumes a singleton tuple around
> >it. 
> 
> I don't think I can remember ever being tripped up
> by this. I don't see it as enough of a problem to
> be worth changing anything -- particularly not as
> drastic as adding a new operator or deprecating
> any existing one.

  I certainly have been, several times over the years.  While I think I've
learned my lesson now, it continues to be a problem when using libraries
other people have written.  Unfortunately, the below suggestion doesn't
resolve this aspect of the problem.

  I can wishfully think that this change is no bigger than the one PEP 238
has made, and so will not be rejected out of hand.  To throw some more
random (but applicable) zen out there:

    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    ...
    Special cases aren't special enough to break the rules.

  (Yes, I'm aware of the next line ;)

> 
> If you find it a problem, you can easily write a
> small function which behaves the way you want:
> 
> >>> def fmt(s, *a):
> ...  return s % a
> ...
> >>> fmt("abc %s def", 1)
> 'abc 1 def'
> >>> fmt("abc %s def", (1, 2))
> 'abc (1, 2) def'
> >>> fmt("abc %s def %s ghi", 1, 2)
> 'abc 1 def 2 ghi'
> 

  Jp

-- 
 up 9 days, 17:59, 7 users, load average: 0.29, 0.10, 0.03





More information about the Python-list mailing list