[Python-Dev] transitioning from % to {} formatting

Vinay Sajip vinay_sajip at yahoo.co.uk
Thu Oct 1 08:59:40 CEST 2009

Vinay Sajip <vinay_sajip <at> yahoo.co.uk> writes:

> #Just typing this in, it's not tested or anything
> class DollarMessage:
>     def __init__(self, fmt, *args, **kwargs):
>         self.fmt = fmt
>         self.args = args
>         self.kwargs = kwargs
>     def __str__(self):
>         return string.Template(self.fmt).substitute(*args, **kwargs)

Whoops, sorry, pressed the "post" button by accident on my previous post. The
above substitute call should of course say

string.Template(self.fmt).substitute(*self.args, **self.kwargs)

and you can alias DollarMessage (or whatever name you choose) as _ or __, say.

As far as the Formatter formatting goes, it's easy enough to subclass Formatter
to format using whatever approach you want.


Vinay Sajip

More information about the Python-Dev mailing list