[Python-3000] print() parameters in py3k

Guido van Rossum guido at python.org
Tue Nov 21 17:18:34 CET 2006


On 11/21/06, Fredrik Lundh <fredrik at pythonware.com> wrote:
> 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.

Hm. While not as obviously from a different universe as Barry's
proposal, this still pretty weird, probably at least from a different
planet (much farther than Iceland anyway :-). Treating the first
argument differently based on its being of a specific type doesn't
sound right to me; what if you are handed an object x and you decide
to print it using print(x), but surreptitiously (or by mistake) they
hand you a format object?

I do support adding printf(), or printf()-like capabilities. But I
suggest to leave this for a separate PEP. After all, one of the
arguments PEP 3105 mentions for making print() a function is that it
opens the door for printf()...

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list