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

Steven Bethard steven.bethard at gmail.com
Fri Oct 2 03:13:12 CEST 2009

On Thu, Oct 1, 2009 at 4:35 PM, Brett Cannon <brett at python.org> wrote:
> On Thu, Oct 1, 2009 at 15:19, Steven Bethard <steven.bethard at gmail.com> wrote:
>> On Thu, Oct 1, 2009 at 11:03 AM, Brett Cannon <brett at python.org> wrote:
>>> 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.
>> Could someone please lay it out for me, step by step, showing what
>> happens in each version?
> 1. Nothing changes as hopefully the wrapper works fine (as people are
> pointing out, though, my approach needs to override __str__() to
> return 'self', else the str type will just return what it has
> internally in its buffer).
> 2. Raise a deprecation warning when ``isinstance(ob, brace_fmt)`` is
> false. When a class is passed in that is a subclass of brace_fmt, call
> ob.format() on it.
> 3. Require the subclass.
> 4. Remove the requirement and always call ob.format().

Thanks Brett, that's clear. So you save one version over the proposal
of adding a format= flag to the API.

On Thu, Oct 1, 2009 at 4:13 PM, James Y Knight <foom at fuhm.net> wrote:
> Here's what I said in my first message, suggesting this change. Copy&pasted
> below:
[snip steps that only talk about str.__mod__, not an API that uses it]

I didn't understand how you wanted to apply your suggestion to an API
(instead of str.__mod__) the first time and I still don't understand
it. Is what Brett has proposed the same thing?

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

More information about the Python-Dev mailing list