[Python-3000] More PEP 3101 changes incoming

James Y Knight foom at fuhm.net
Sun Aug 12 20:28:10 CEST 2007


I've been skimming a lot of the discussion about how to special case  
various bits and pieces of formatting, but it really seems to me as  
if this is really just asking for the use of a generic function. I'm  
not sure how exactly one spells the requirement that the first  
argument be equal to a certain object (e.g. 'r') rather than a  
subtype of it, so I'll just gloss over that issue.

But anyways, it seems like it might look something like this:

# Default behaviors
@overload
def __format__(format_char:'r', obj, format_spec):
   return __format__('s', repr(obj), format_spec)

@overload
def __format__(format_char:'s', obj, format_spec):
   return __format__('s', str(obj), format_spec)

@overload
def __format__(format_char:'f', obj, format_spec):
   return __format__('s', float(obj), format_spec)

@overload
def __format__(format_char:'d', obj, format_spec):
   return __format__('s', int(obj), format_spec)


# Type specific behaviors
@overload
def __format__(format_char:'s', obj:str, format_spec):
   ...string formatting...

@overload
def __format__(format_char:'f', obj:float, format_spec):
   ...float formatting...

@overload
def __format__(format_char:'d', obj:int, format_spec):
   ...integer formatting...

James


More information about the Python-3000 mailing list