[Python-3000] More PEP 3101 changes incoming
Eric Smith
eric+python-dev at trueblade.com
Mon Aug 13 06:30:01 CEST 2007
Adam Olsen wrote:
> My proposal was to flip this logic: __format__ should check for its
> own specifiers first, and only if it doesn't match will it return
> NotImplemented (triggering a call to __int__, or maybe __index__).
>
> class MyInt:
> def __format__(self, spec):
> if is_custom_spec(spec):
> return "MyInt instance with custom specifier " + spec
> return NotImplemented
> def __int__(self):
> return <some local state>
>
> This avoids the need for a public is_int_specifier. unicode.format
> would still have the logic, but since it's called after you're not
> restricted from starting with d, x, etc.
That makes sense, since the object would have first crack at the spec,
but needn't implement the conversions itself. Let me see where that
gets me. Now I see what you were getting at with your earlier posts on
the subject. It wasn't clear to me that the "use a fallback" would
include "convert based on the spec, if possible".
If accepted, this should go into the PEP, of course.
It's not clear to me if __int__ or __index__ is correct, here. I think
it's __int__, since float won't have __index__, and we want to be able
to convert float to int (right?).
Thanks!
Eric.
More information about the Python-3000
mailing list