"print" as function not statement

Peter Otten __peter__ at web.de
Wed Mar 10 07:52:46 EST 2004


Aahz wrote:

> Your arguments are persuasive, but I disagree with your conclusions.
> Given that almost every programming language has something like "print"
> built into it, we can't afford to drop that name.  I think you need to
> find a path that permits mutation from a reserved word.

Yes. The only ambiguity I can think of would be tuple literals.

> The other issue is that you're wrong to make print() a function.
> Instead, it should be a callable object.  That way, the default print
> object has state:
> 
>     print(a, b, c)
>     print.set_output(my_file)
>     print(a, b, c)
>     print(x, y, z)
>     print.set_separator(None)
>     print("The value is: ", 3)

My first thought was to add separator and newline attributes to file objects
and keep print just like it is, but the print object is more flexible. You
could easily add further extensions, e. g.

print.set_format(float, "%.2f")
print.set_format(str, "%r")

I'm not sure what to do with different printers operating on the same file -
most likely sys.stdout. Should they share (some) state like today's
softspace, should they operate as if they were the only one or should
multiple printers be disallowed?

Another thing that is not addressed is how to suppress newline. I can think
of

1 a method: print.write(1, 2)
2 a keyword argument print(1, 2, newline=False)
3 a special marker object: print(1, 2, suppress_newline)
4 make no newline the default

I tend to option 1 but don't particularly like it either.

Peter 

PS: Actually, when I started to write I thought I knew how it should be
done...




More information about the Python-list mailing list