gratuitous new features in 2.0

Eric Lorenzo zo at angband.org
Fri Aug 25 13:03:51 EDT 2000


ge at nowhere.none (Grant Edwards) writes:
> Personally, I don't understand why "print" exists at all.  You
> can accomplish the same thing with sys.stdout.write().  I think
> "print" does way to many automagical things behind the back of
> the programmer:
> 
>  1) converts objects to strings
>  
>  2) implicitly writes to a global object
>     (I've always been warned about such side-effects)
>     
>  3) writes a newline

I don't mind that it does these automagical things: They're common
things to want to do.  I do mind that 'print' is a statement and a
special syntax zone, when giving it this status does nothing to help
it do these automagical things

A cleaner solution would have been to add 'print' and 'println'
methods to the base file object (see my other article with
'pretty_print' and 'pretty_println').  Then for convenience in simple
scripts and in cases where setting sys.stdout is an appropriate
solution, add builtin functions that amount to:

    def print(*items):
        return apply(sys.stdout.print, items)

    def println(*items):
        return apply(sys.stdout.println, items)


Giving print its own special syntax has other disadvantages, since it
now doesn't play so well with other (functional) parts of the language
that accept callable objects.  For example: If we had print/println
builtin functions instead of the print statement, and needed to print
a variable-length group of items, one could simply do:

    apply(println, l)

As things stand, we get:

    for x in l:
        print x,
    print

This isn't a huge savings, but I do find the first considerably more
readable.  It also has better maintainability since it requires one
less variable in the local namespace.

> But, I'm sure it's too late to get rid of the print statement.

I agree.

Eric



More information about the Python-list mailing list