<br><br><div><span class="gmail_quote">On 11/20/06, <b class="gmail_sendername">Barry Warsaw</b> <<a href="mailto:email@example.com">firstname.lastname@example.org</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
-----BEGIN PGP SIGNED MESSAGE-----<br>Hash: SHA1<br><br>On Nov 20, 2006, at 1:21 PM, Guido van Rossum wrote:<br><br>> On 11/20/06, Barry Warsaw <<a href="mailto:email@example.com">firstname.lastname@example.org</a>> wrote:<br>>> I'd like to at least see a discussion of a more printf() or logging
<br>>> style for print function.<br>><br>> Agreed, as long as it isn't presented as an alternative to the more<br>> basic print() function. We don't want to force students making their<br>> first forays into programming to have to learn format strings. Having
<br>> printf() for more advanced use together with print() as defined in PEP<br>> 3105 makes sense to me.<br><br>That seems reasonable to me too (separate print() and printf()<br>thingies).<br><br>>> Specifically, I wonder if it wouldn't be
<br>>> useful to require a format string with positional and keyword<br>>> arguments being used for automatic interpolation. E.g.<br>>><br>>> print('%s: %s', field, value)<br>>><br>>> instead of
<br>>><br>>> print('%s: %s' % (field, value))<br>><br>> Before going too far down this route, first read PEP 3101, which<br>> proposes a different way out of the issues around the % operator. (Not<br>
> saying we couldn't adopt PEP 3101 *and* a printf() function.<br><br>Thanks for the pointer. I agree that if we adopt a companion printf<br>() function it should definitely require (only) PEP 3101 format strings.<br>
<br>>> Another thought: what if 'print' weren't a function but a callable<br>>> object. exposed in builtins. I'm thinking something roughly parallel<br>>> to stdout, stderr, stdin, or maybe better cout, cerr, cin.
<br>><br>> I'm not sure I follow the difference. It was quite clear from earlier<br>> discussions that we *don't* want print to be a bound method of a<br>> hypothetical print method on sys.stdout; that's much less flexible,
<br>> and makes it much more work to implement a file-like type. print()<br>> should dynamically look up sys.stdout each time it is called (and<br>> exactly once per call).<br>><br>> (BTW I'm not sure why you like cin/cout/cerr better than stdin/
<br>> stdout/stderr?)<br><br>I was trying to make an analogy (probably poorly so) that 'print'<br>would be bound to an instance as opposed to a function (current<br>proposal) or structure-like thingie without methods as in std*.
<br><br>>> The default 'print' object then would be a "print-to-sys.stdout-with-<br>>> \n", but you would then be able to do something like:<br>>><br>>> stderr = print.clone(stream=sys.stderr
)<br>>> stderr('here go all my error messages')<br>>><br>>> or<br>>><br>>> smtpout = print.clone(stream=mysock, end='\r\n')<br>>> smtpout('$code $msg', code=250, msg='Okay')<br>><br>
> I'd like to firmly resist adding more mechanism for somethign that can<br>> be addressed so simply by a tiny wrapper function:<br>><br>> def smtpout(*args):<br>> print(*args, file=mysock, end="\r\n")
<br><br>What I was trying to address was the namespace collisions you'd get<br>between printf-style keyword arguments and 'special' arguments that<br>the print function would accept. The thing is that if we were to add<br>
both a print and a printf, you'd like for their interfaces to be as<br>similar as possible except that the latter takes a format string as<br>its first argument. Clearly 'special' arguments like 'file' and<br>'end' will prohibit their use as keyword arguments for printf, and I
<br>don't like that (this isn't anything new -- the problem crops up<br>occasionally in other places, see the email package API).</blockquote><div><br>Why do they need to be similar? print() is just a really simple, quick output function. printf() seems to be proposed for advanced formatting needs for sending to stdout. As Guido said, the newline should be explicit in printf(), so that already begins to deviate from print(). And 'sep' would be useless. So all that leaves is 'file', and that is just convenience for passing a string to a 'write' method. And for that I think people can just learn not to use 'file' as a name of a variable. Or we can just lose that as well and have printf() be nothing more than a convenience function that avoids having to specify the method name and sending it to print with the desired arguments.
<br><br>Then again, this to me seems to make printf() not that grand to lead to its own built-in. But I have been fine with the way things have been so far anyway with having a separate line that stores some string I created before calling 'print'. If you don't have any keyword arguments then printf() is just::
<br><br> def printf(format_string, *args, **kwargs):<br> output = format_string.format(*args, **kwargs)<br> print(output, end='')<br><br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
One way out would be to use special argument names, e.g. prepending<br>and or appending some number of underscores. This doesn't eliminate<br>the problem, but it reduces its likelihood.</blockquote><div><br>I don't see this being a big problem if the only argument that is common is 'file'.
<br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> Another approach would<br>for 'print' to be an object with attributes that would change
<br>behavior, such as the stream to write to or line-ending characters.</blockquote><div><br>Yuck. Why would you want to suddenly set global semantics in a built-in instead of a module? That's a total shift in how Python does things at a global level. Plus I don't want to have to worry about setting 'sep' or 'end' for every function that uses print() just because some other function decided that it didn't like single spaces but wanted to use tabs for a separator.
<br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">A possible third approach might be some special singleton objects<br>that, when seen positionally would modify the state of the underlying
<br>print object.</blockquote><div><br>Yuck again. This seems to be getting rather specific just for printing when we have already lived without all of this functionality for so long with minor issue.<br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Of the three (maybe fourth: "sorry you can't use 'end'<br>or 'file'"),</blockquote><div><br>I like this option. =) <br></div><br>-Brett<br></div>