[Python-3000] More PEP 3101 changes incoming

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Aug 14 03:10:42 CEST 2007


Eric Smith wrote:
> In order for me to write the __format__ function in MyInt, I have to 
> know if the specifier is in fact an int specifier.
 >
 > class MyInt:
 >      def __format__(self, spec):
 >          if int.is_int_specifier(spec):
 >              return int(self).__format__(spec)
 >          return "MyInt instance with custom specifier " + spec

I would do this the other way around, i.e. first look
to see whether the spec is one that MyInt wants to handle
specially, and if not, *assume* that it's an int specifier.
E.g. if MyInt defines a new "m" format:

   def __format__(self, spec):
     if spec.startswith("m"):
       return self.do_my_formatting(spec)
     else:
       return int(self).__format__(spec)

--
Greg


More information about the Python-3000 mailing list