PEP 378: Format Specifier for Thousands Separator

nn pruebauno at latinmail.com
Thu May 23 09:44:05 EDT 2013


On May 22, 6:31 pm, Carlos Nepomuceno <carlosnepomuc... at outlook.com>
wrote:
> ----------------------------------------
>
> > Date: Wed, 22 May 2013 13:26:23 -0700
> > Subject: Re: PEP 378: Format Specifier for Thousands Separator
> > From: prueba... at latinmail.com
> > To: python-l... at python.org
> [...]
>
> > Maybe a cformat(formatstring, variables) function should be created
> > in the string module so people who prefer that can use it. I don't
> > mind the C formatting syntax but I don't like the fact that the %
> > operator does something totally different when the first variable is
> > an integer and the fact that it misbehaves if the second variable is a
> > tuple.
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
> I still don't understand why % benefits from literals optimization ("'%d'%12345") while '{:d}'.format(12345) doesn't.
>
> What "totally different" you talking about? Please give me an example.

>>> def eggs(spam, ham): return spam % ham

>>> def milk(beef, steak): return beef.format(steak)

>>> a='%s'
>>> c=9
>>> d=4
>>> e=[1,2]
>>> f=(3,5)
>>> d='{}'

>>> eggs(a,4)
'4'
>>> eggs(c,4)
1
>>> eggs(a,e)
'[1, 2]'
>>> eggs(a,f)
Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    eggs(a,f)
  File "<pyshell#1>", line 1, in eggs
    def eggs(spam, ham): return spam % ham
TypeError: not all arguments converted during string formatting
>>> '%s'%(5%3)
'2'

>>> milk(d,4)
'4'
>>> milk(c,4)
Traceback (most recent call last):
  File "<pyshell#53>", line 1, in <module>
    milk(c,4)
  File "<pyshell#49>", line 1, in milk
    def milk(beef, steak): return beef.format(steak)
AttributeError: 'int' object has no attribute 'format'
>>> milk(d,e)
'[1, 2]'
>>> milk(d,f)
'(3, 5)'
>>> '{}'.format(5%3)
'2'




More information about the Python-list mailing list