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

Steven Bethard steven.bethard at gmail.com
Fri Oct 2 00:19:38 CEST 2009

On Thu, Oct 1, 2009 at 11:03 AM, Brett Cannon <brett at python.org> wrote:
> So I created this last night:
> import collections
> class braces_fmt(str):
>    def __mod__(self, stuff):
>        if isinstance(stuff, tuple):
>            return self.__class__(self.format(*stuff))
>        elif isinstance(stuff, collections.Mapping):
>            return self.__class__(self.format(**stuff))
>        else:
>            return self.__class__(self.format(stuff))
> The biggest issue is that ``"%s" % {'a': 42}`` substitutes the dict
> instead of throwing an error that str.format() would do with the code
> above. But what's nice about this is I think I can use this now w/ any
> library that expects % interpolation and it should basically work.

I see how this could allow a user to supply a {}-format string to an
API that accepts only %-format strings. But I still don't see the
transition strategy for the API itself. That is, how does the %-format
API use this to eventually switch to {}-format strings? Could someone
please lay it out for me, step by step, showing what happens in each

Where did you get that preposterous hypothesis?
Did Steve tell you that?
        --- The Hiphopopotamus

More information about the Python-Dev mailing list