conditionals in lambdas?

Michael P. Soulier msoulier at nortelnetworks.com
Fri Nov 3 15:18:23 EST 2000


In article <KGEM5.4144$jv2.474395 at newsc.telia.net>, Fredrik Lundh wrote:
>
>nope.  lambdas can only contain simple expressions.
>
>also see:
>
>    http://www.python.org/doc/FAQ.html#6.9
>    6.9. Why can't lambda forms contain statements?
>
>    "...the only advantage of using a lambda form instead of
>    a locally-defined function is that you don't need to invent
>    a name for the function..."

    Thanks. I guess I'm still in that Perl mentality where I'm trying to do
many things on one line. 

    I was looking for a simple way of grabbing all lines that began with a
the string #LOADDATA. In perl I'd do this:

open (FILE, "file") or die "Can't open file: $!";
@contents = grep { /^\#LOADDATA/ } <FILE>;

    In python I'm doing it this way...

filecontents = open(filename, "r").readlines()
filecontents = filter(filterloaddata, filecontents)

def filterloaddata(string):
    if string[:9] == '#LOADDATA':
        return 1
    else:
        return 0

    I guess it'd just be nice to do it without the function. I'm still
learning Python though...

    Mike

-- 
Michael P. Soulier, TD12, SKY  Tel: 613-765-4699 (ESN: 39-54699)
Optical Networks, Nortel Networks, SDE Pegasus
"...the word HACK is used as a verb to indicate a massive amount
of nerd-like effort."  -Harley Hahn, A Student's Guide to UNIX
Nortel Linux User's Group Ottawa: (internal) http://nlug.ca.nortel.com



More information about the Python-list mailing list