n = lambda x: print x

Michael Hudson mwh21 at cam.ac.uk
Thu May 4 15:52:29 CEST 2000


"Warren Postma" <embed at geocities.com> writes:

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

It's not a function, it's a statement (like exec, though that was once
a function).  And you can't have statements in lambda expressions.

FWIW, "lambda" binds tighter than ",":

>>> lambda x:1,1
(<function <lambda> at 812a220>, 1)

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

>From http:starship.python.net/crew/amk/quotations/python-quotes.html:
 
  I mean, just take a look at Joe Strout's brilliant little "python
  for beginners" page. Replace all print-statements with
  sys.stdout.write( string.join(map(str, args)) + "\n") and you'll
  surely won't get any new beginners. And That Would Be A Very Bad
  Thing.
                                         Fredrik Lundh, 27 Aug 1996 

I.e. print is a convenience.  Python generally emphasises consistency
over convenience, but in this case, the convenience functionality is
so convenient it has been included.

> Why did Guido not parenthesize arguments to PRINT when designing Python?

Maybe so you can do

>>> print 1,

? I don't really know.

> Look, a Syn! Otherwise, this language is perfect. 

No, it isn't; but there's another quote about Python that I like very
much (I have lots of these):

  Python is a language that gets its compromises exactly right.

I think that on was Don Beaudry, but I'm not sure.

I'd like multimethods, myself.

> ;-) I love Python.

So do I.

Cheers,
Michael

-- 
  I saw `cout' being shifted "Hello world" times to the left and
  stopped right there.
                                                    -- Steve Gonedes



More information about the Python-list mailing list