[Python-Dev] Backporting PEP 3101 to 2.6 (original) (raw)
Eric Smith eric+python-dev at trueblade.com
Thu Jan 10 14:31:54 CET 2008
- Previous message: [Python-Dev] PEP: Post import hooks
- Next message: [Python-Dev] Backporting PEP 3101 to 2.6
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
(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.
- Previous message: [Python-Dev] PEP: Post import hooks
- Next message: [Python-Dev] Backporting PEP 3101 to 2.6
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]