| Store | Cart

"print" as function not statement

From: Mark Kamikaze Hughes <kami...@kuoi.asui.uidaho.edu>
8 Mar 2004 01:36:28 GMT
Paul Prescod <paul at prescod.net>
wrote on Sun, 07 Mar 2004 16:57:29 -0800:
> Problems with "print"> 	New users are constantly asking how to print elements without> 	trailing whitespace.

  And they are then constantly told to use formatted strings, which are
the next logical step up:
print "%s%s" % (a, b)

  If the trailing newline is the problem:
sys.stdout.write("%s%s" % (a, b))

> 	The use of a trailing comma to disable newline has no clear> 	relation to any other syntax elsewhere in Python.

  It is familiar to BASIC programmers, but it's certainly not a pythonic

> 	The cheveron syntax is similarly unique.

  (">>object", aka "chevron", for those who don't recognize that term)

  It is familiar to Unix programmers for redirecting output.  It's not
pythonic, but not entirely alien.

> 	"print" is the one of only two statement-generating keywords> 	that is not a flow-control or scope-control operator.

  Why does this matter?

> 	Because print is a statement, it cannot be used in contexts> 	like list comprehensions and lambda functions.

  You can use sys.stdout.write() instead, or if you like print's
intelligent spaces, you write a minimal utility function:
def show(s):
    print s,
    return s

> 	The word "print" is very common in English usage and would be> 	very useful for method and function names. It is  a (very)> 	long-term goal of this PEP to free it up for this purpose.

  That will never happen, because it would break an enormous amount of

  "print" means "display on the console" in most programming languages.
Printing to paper is uncommon and hopefully becoming less common.  What
else are you going to do in a function called "print"?

> 	"print" is one of the first thing a Python newbie learns but> 	learning it teaches very little about the rest of the language.> 	All of the idiosyncratic syntax you learn will never be seen> 	again elsewhere in the language and 99.9% of non-control flow> 	operations are performed with functions, not statements.

  Newbies are not taught the entire syntax of print at once.  They learn
to assemble a few comma-delimited items, and it "just works" at putting
spaces and newlines in the right place.

  The more advanced uses were probably inappropriate additions.
Eliminating those so print is a simple construct again would be better
than removing and replacing it, but I don't see a convincing case for

> Proposed solution: "show"> > 	I propose a new built-in function called "show". It can take an> 	arbitrary number of positional arguments and three optional> 	keyword arguments:


  Anything print isn't suitable for at present, sys.stdout.write() and
formatted strings are.

 <a href="http://kuoi.asui.uidaho.edu/~kamikaze/"> Mark Hughes </a>
"Doing the impossible makes us mighty." -Captain Malcolm Reynolds, Firefly

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