[Python-3000] print() parameters in py3k

Fredrik Lundh fredrik at pythonware.com
Tue Nov 21 12:05:28 CET 2006


Adam Olsen wrote:

>> (1)  Is this an explicit rejection of a keyword for a format string, such as
>>
>>     print (x, y, z, fmt="My X: %s, Your Y: %s, His Z: %s")
> 
> Although odd, this appeals to me somehow.  It avoids the risk of
> passing an untrusted string as the format (even if that's much less
> dangerous than in C).

it doesn't read well, though.

but if we take the print, printf, and str.format trojka, the rather 
common mistake you mention, and my preference for designing API:s that 
have room for performance optimizations, and refactor things a bit, we 
can do as follows:

1) move formatting to a separate object.

     str.format(args)

becomes one or more of

     format(str).format(args) # object style
     format(str)(args) # partial application style
     format(str) % (args) # old-style

this allows the formatter object to parse the formatting string (and 
possibly also cache it), and also opens up for alternative target API:s 
(e.g. tostringlist, etc).

and the format object should somewhat modular and fully subclassable, of 
course, so Barry can prototype all his ideas all on his own ;-)

2) for convenience, extend print to treat a format object as a format 
specifier for the following arguments:

     print (x, y, z, fmt="My X: %s, Your Y: %s, His Z: %s")

becomes

     print(format("My X: %s, Your Y: %s, His Z: %s"), x, y ,z)

3) get rid of printf.

4) go to Iceland and optimize the heck out of format/print.

</F>



More information about the Python-3000 mailing list