New Python 3.0 string formatting - really necessary?
Aaron Brady
castironpi at gmail.com
Sun Dec 21 18:37:04 EST 2008
On Dec 21, 10:58 am, MRAB <goo... at mrabarnett.plus.com> wrote:
> Aaron Brady wrote:
> > On Dec 21, 10:31 am, MRAB <goo... at mrabarnett.plus.com> wrote:
snip
> >> The original format is a string. The result of '%' is a string if
> >> there's only 1 placeholder to fill, or a (partial) format object (class
> >> "Format"?) if there's more than one. Similarly, the format object
> >> supports '%'. The result of '%' is a string if there's only 1
> >> placeholder to fill, or a new (partial) format object if there's more
> >> than one.
>
> >> >>> f = "%r %i"
> >> >>> type(f)
> >> <type 'str'>
> >> >>> f = f % (2, 3, 4)
> >> >>> type(f)
> >> <type 'Format'>
> >> >>> f = f % 1
> >> >>> type(f)
> >> <type 'str'>
>
> > Alright, so how are you handling:
>
> >>>> f= "%s %i"
> >>>> type( f )
> > <type 'str'>
> >>>> f= f% '%i' #now '%i %i'
> >>>> type( f )
> > <type 'Format'>
> >>>> f= f% 1
> >>>> type( f )
> > ?
>
> > In other words, are you slipping '1' in to the very first available
> > slot, or the next, after the location of the prior?
>
> Let's assume that Format objects display their value like the equivalent
> string format:
>
> >>> f = "%r %i"
> >>> f
> '%r %i'
> >>> f = f % (2, 3, 4)
> >>> f
> <Format '(2, 3, 4) %i'>
> >>> f = f % 1
> >>> f
> '(2, 3, 4) 1'
> >>>
> >>> f = "%s %i"
> >>> f
> '%s %i'
> >>> f = f % '%i'
> >>> f
> <Format '%%i %i'>
> >>> f = f % 1
> >>> f
> '%%i 1'
I assume you meant '%i 1' since there are no more flags in f, and it's
returned to a regular string.
'f %= 1' doesn't work any more as in-place modulo, since one time, 'f'
is a Format object, the other, 'f' is a string. Just raise an
exception for that (or assign to __class__ IINM if I'm not mistaken).
Actually, the class you showed is kind of nifty. Tuples are correctly
interpolated. I think on the whole you'll use more parenthesis, since
each term in the tuple appears separately, and might be an expression
(have a lower-precedence op.), as well as more modulo signs.
You can currently do-it-yourself, you just need a constructor in the
format string.
>>> f = Format("%r %i")
>>> type(f)
<type 'Format'>
>>> f = f % (2, 3, 4)
>>> type(f)
<type 'Format'>
Or, as someone suggested earlier, a new literal marking:
>>> f = f"%r %i"
>>> type(f)
<type 'Format'>
More information about the Python-list
mailing list