Random ...
 
December 2017
S M T W T F S
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Tags ...
Links



python+zope++:: lambda: What the heck are ye?
Posted at 30.Nov,2005 13:41  Comments 3 / Trackbacks 0 / Like this post!
Technorati tag(s):

I can never grok lambda. I get the simple examples; such as:

  >>> def f (x): return x**2
          ... 
  >>> print f(8)

   64

  >>> 
  >>> g = lambda x: x**2
  >>> 
  >>> print g(8)

  64

But today, while trying to get files using ftplib, I came across this snippet from effbot

and I quote:

  import ftplib
  import sys

  def gettext(ftp, filename, outfile=None):
    # fetch a text file
    if outfile is None:
        outfile = sys.stdout
    # use a lambda to add newlines to the lines read from the server
    ftp.retrlines("RETR " + filename, lambda s, w=outfile.write: w(s+"\n"))

Notice the last lines? Can anybody turn that into a working understandable function (for the last line)? maybe I'm just dense. :P

I have tried, but can't seem to get to how to use lambda.

After further review [1 Dec 2005]

I think I get it now. As I never had the pleasure of using lambda before, and me thinking I'd never use it in this lifetime, I never even looked at lambda thoroughly.

Well, here's the gist of it; a lambda:

  • syntax is:
         lambda arg1, arg2,..argn: expression
    
  • can only have one expression; no statement(s) allowed
  • always return the expression
  • can be used where statements cannot; eg in lists and dicts [1]

[1] Programming Python rev 1 pg 214

A few things I learned::

  • lambda is something I might use now (sparingly of course)
  • books give better result when a problem (or two) is applied to it since we can actually look for the keywords (as the above points elaborate). Otherwise, it's like reading a newspaper :))
  • books need better formatting; something like the above points, methinks

python


Bookmark and Share

Is this entry helpful? Comments/Donate/Click some google ads.  
Trackback is http://myzope.kedai.com.my/blogs/kedai/55/tbping 

Comments
Don't need it

I can't tell for sure because the lambda is redundent for my version of python. Running the codes like this :-
ftp.retrlines("RETR " + filename)
gives same output

From the code I guess that the lamda append "/n" at end of the output. In my python (ver 2.4.2) I don't need to append the "/n" any more. It is done by default.

p/s: I know that I'm not ansering your question which is to convert the said lamda to def

p/ss: python rocks

linuxlah  at 30.Nov,2005 17:31
output

well, the default callback is '\n', but without the lambda, I don't get the contents of the files.

the output is only to stdout.

maybe i'm missing something.

p/s - yes, python really rocks

kedai  at 30.Nov,2005 18:49
my solution

my solution:-
http://linuxlah.blogspot.com/2005/11/python-lamda-def-for-kedaibahtiar.html

hari nie I'm bored to stayback at my office :)

linuxlah  at 30.Nov,2005 19:22
Post a comment