"print" as function not statement

Mark 'Kamikaze' Hughes kamikaze at kuoi.asui.uidaho.edu
Sun Mar 7 20:36:28 EST 2004


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
idiom.

> 	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
code.

  "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
it.

> 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:

-1

  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



More information about the Python-list mailing list