Partial string formatting?

Jp Calderone exarkun at intarweb.us
Tue Jan 21 14:43:26 EST 2003


On Tue, Jan 21, 2003 at 01:26:57PM -0600, Jeff Epler wrote:
> On Tue, Jan 21, 2003 at 04:18:55PM +0000, Gon?alo Rodrigues wrote:
> [snip]
> 
> I think the following function 'format(s, m)' will work.  I tested it a
> little bit.
> 
> def format(s, d):
>     chunks = s.split('%')
>     out = [chunks[0]]
>     for f in chunks[1:]:
>         f = "%" + f
>         try:
>             out.append(f % d)
>         except KeyError:
>             out.append(f)
> 
>     return "".join(out)

  It chokes on %%, but that's easily fixed.  This solution makes me wonder
(once again) if being able to override the % behavior in str subclasses
would be worthwhile.  It's slightly better than my use-case of simply being
able to pass "format string".__mod__ around.

  Jp

-- 
"There is no reason for any individual to have a computer in their
home."
                -- Ken Olson, President of DEC, World Future Society
                   Convention, 1977
--
 12:00am up 36 days, 9:48, 4 users, load average: 0.10, 0.14, 0.14
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030121/9357a326/attachment.sig>


More information about the Python-list mailing list