[Python-Dev] transitioning from % to {} formatting (original) (raw)
Steven Bethard steven.bethard at gmail.com
Fri Oct 2 00:19:38 CEST 2009
- Previous message: [Python-Dev] transitioning from % to {} formatting
- Next message: [Python-Dev] transitioning from % to {} formatting
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 bracesfmt(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 version?
Steve
Where did you get that preposterous hypothesis? Did Steve tell you that? --- The Hiphopopotamus
- Previous message: [Python-Dev] transitioning from % to {} formatting
- Next message: [Python-Dev] transitioning from % to {} formatting
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]