| Store | Cart

"print" as function not statement

From: Peter Otten <__pe...@web.de>
Wed, 10 Mar 2004 13:52:46 +0100
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...

Recent Messages in this Thread
Mark Kamikaze Hughes Mar 08, 2004 01:36 am
John Roth Mar 08, 2004 02:38 am
Leif K-Brooks Mar 08, 2004 07:32 am
Duncan Booth Mar 08, 2004 08:31 am
Leif K-Brooks Mar 08, 2004 10:36 am
A.M. Kuchling Mar 08, 2004 01:29 pm
Heather Coppersmith Mar 08, 2004 12:56 pm
David MacQuigg Mar 09, 2004 09:04 pm
Aahz Mar 10, 2004 10:43 am
Peter Otten Mar 10, 2004 12:52 pm
David MacQuigg Mar 10, 2004 03:32 pm
Messages in this thread