gratuitous new features in 2.0

Niels Diepeveen niels at endea.demon.nl
Fri Aug 25 14:11:04 EDT 2000


Guido van Rossum schreef:

> I feel that this debate is really about whether print should have been
> a function or method rather than a statement.  If you are in the
> function camp, of course adding special syntax to the existing print
> statement is not something you like.  I suspect the objection to the
> new syntax comes mostly from people who already think that the print
> statement was a bad idea.  Am I right?

Here is one counterexample. I think the print statement is convenient
enough for some purposes to merit special syntax. However, the whole
point of it, as far as I'm concerned, is that you don't have to specify
where the output goes every time. I use it sometimes for simple
interactive stuff, but mostly for quickly adding a trace point here and
there. For this purpose it's perfect, particularly because I don't have
to import sys everywhere. If I have to specify a file object anyway, I
can just as easily call its write method and not worry about spaces
going where I don't really want them.

I would much prefer making redirecting output from the print statement
easier by separating it from sys.stdout. Why not provide a function
sys.printto() [or possibly __builtin__.printto()] that will redirect
output from the print statement, but does not change sys.stdout?

def printto(file=None):
    global _print_file
    _print_file = file

def print(*args):
    # Simplified pseudocode for the print statement
    # NOT an actual function
    if sys._print_file is None:
        file = sys.stdout # for backward compatibility
    else:
        file = sys._print_file
    for s in args:
        file.write(s)

I can't speak for anyone else, but this would suit me much better. I
also think, not entirely objectively, that this is at least as easy to
explain as the extended print syntax, and certainly easier to remember.

-- 
Niels Diepeveen
Endea automatisering




More information about the Python-list mailing list