[Python-Dev] Backporting PEP 3101 to 2.6
Eric Smith
eric+python-dev at trueblade.com
Thu Jan 10 14:31:54 CET 2008
(I'm posting to python-dev, because this isn't strictly 3.0 related.
Hopefully most people read it in addition to python-3000).
I'm working on backporting the changes I made for PEP 3101 (Advanced
String Formatting) to the trunk, in order to meet the pre-PyCon release
date for 2.6a1.
I have a few questions about how I should handle str/unicode. 3.0 was
pretty easy, because everything was unicode.
1: How should the builtin format() work? It takes 2 parameters, an
object o and a string s, and returns o.__format__(s). If s is None, it
returns o.__format__(empty_string). In 3.0, the empty string is of
course unicode. For 2.6, should I use u'' or ''?
2: In 3.0, object.__format__() is essentially this:
class object:
def __format__(self, format_spec):
return format(str(self), format_spec)
In 2.6, I assume it should be the equivalent of:
class object:
def __format__(self, format_spec):
if isinstance(format_spec, str):
return format(str(self), format_spec)
elif isinstance(format_spec, unicode):
return format(unicode(self), format_spec)
else:
error
Does that seem right?
3: Every overridden __format__() method is going to have to check for
string or unicode, just like object.__format() does, and return either a
string or unicode object, appropriately. I don't see any way around
this, but I'd like to hear any thoughts. I guess there aren't all that
many __format__ methods that will be implemented, so this might not be a
big burden. I'll of course implement the built in ones.
Thanks in advance for any insights.
Eric.
More information about the Python-Dev
mailing list