[Python-3000] print() parameters in py3k

Guido van Rossum guido at python.org
Mon Nov 20 19:21:34 CET 2006


On 11/20/06, Barry Warsaw <barry at python.org> wrote:
> I'd like to at least see a discussion of a more printf() or logging
> style for print function.

Agreed, as long as it isn't presented as an alternative to the more
basic print() function. We don't want to force students making their
first forays into programming to have to learn format strings. Having
printf() for more advanced use together with print() as defined in PEP
3105 makes sense to me.

> Specifically, I wonder if it wouldn't be
> useful to require a format string with positional and keyword
> arguments being used for automatic interpolation.  E.g.
>
> print('%s: %s', field, value)
>
> instead of
>
> print('%s: %s' % (field, value))

Before going too far down this route, first read PEP 3101, which
proposes a different way out of the issues around the % operator. (Not
saying we couldn't adopt PEP 3101 *and* a printf() function.

> There are lots of questions to answer, such as whether to use $-
> strings and require keyword arguments.

IMO it should follow PEP 3101 exactly; I think that answers any
questions you might have in this realm.

> Another thought: what if 'print' weren't a function but a callable
> object. exposed in builtins.  I'm thinking something roughly parallel
> to stdout, stderr, stdin, or maybe better cout, cerr, cin.

I'm not sure I follow the difference. It was quite clear from earlier
discussions that we *don't* want print to be a bound method of a
hypothetical print method on sys.stdout; that's much less flexible,
and makes it much more work to implement a file-like type. print()
should dynamically look up sys.stdout each time it is called (and
exactly once per call).

(BTW I'm not sure why you like cin/cout/cerr better than stdin/stdout/stderr?)

> The default 'print' object then would be a "print-to-sys.stdout-with-
> \n", but you would then be able to do something like:
>
> stderr = print.clone(stream=sys.stderr)
> stderr('here go all my error messages')
>
> or
>
> smtpout = print.clone(stream=mysock, end='\r\n')
> smtpout('$code $msg', code=250, msg='Okay')

I'd like to firmly resist adding more mechanism for somethign that can
be addressed so simply by a tiny wrapper function:

def smtpout(*args):
  print(*args, file=mysock, end="\r\n")

> Perhaps you could even hook in i18n by doing something like:
>
> print = print.clone(translate=gettext.gettext)
> print('$who likes to eat $what', person, food)

Ditto**2.

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


More information about the Python-3000 mailing list