n = lambda x: print x

Clarence Gardner clarence at netlojix.com
Thu May 4 16:14:26 CEST 2000

On Thu, 04 May 2000, Warren Postma wrote:
>In a moment of perverse glee, I decided to type the statement in to see what
>it does.  The question I wondered was, if Python accepted this:
>n = lambda x: print x
>What would it do with this:
>n = (lambda x: print x,y)
>Is the comma part of the print statement, or would n become a tuple of two
>values? :-)
>Is the first construct s pecifically disallowed in the python grammar
>somehow? print is a bit of a weirdball function in that it is invoked
>without parenthesis. (The most BASIC-like feature of Python).
>Could we in fact say that if fewer exceptions to rules makes a language
>simpler then the PRINT statement is a bad idea and a PRINT( x,y,z) function
>would have been a much better idea?

The reason you see print as an 'oddball function invoked without parentheses'
is that it is not a function; everywhere else, you refer to it properly as a
statement.  (Which is the reason it can't be used in a lambda: a statement
doesn't have a value like an expression does.)  Having a PRINT function
would be totally redundant, as it would be the same as sys.stdout.write.
The print statement is a handy, built-in tool that I would guess would
surprise most beginners (and users of other very high level languages, or
BASIC) by its absence.

More information about the Python-list mailing list