Home > Software design >  Confused about the Lambda function variable
Confused about the Lambda function variable

Time:09-23

people = ['Dr. Christopher Brooks', 'Dr. Kevyn Collins-Thompson', 'Dr. VG Vinod Vydiswaran', 'Dr. Daniel Romero']\

for person in people:
    print((lambda x: x.split()[0]   ' '   x.split()[-1])(person))

This first for loop returns the list of people with deleted middle, but I'm confused as to why "person" is added at the end? Is this the syntax to say that we mean "x" to be the persons running through the people list?

Furthermore I tried the for loop differently:

for person in people:
    print((lambda person: person.split()[0]   ' '   person.split()[-1]))

this makes a bit more sense to me, but it returns this:

<function <lambda> at 0x7fab41922a60>
<function <lambda> at 0x7fab41922a60>
<function <lambda> at 0x7fab41922a60>
<function <lambda> at 0x7fab41922a60>

Why? Any help is much appreciated, I'm really trying hard to learn this language but it's still so foggy to me!

CodePudding user response:

These lines here:

for person in people:
    print((lambda x: x.split()[0]   ' '   x.split()[-1])(person))

is very poor code because it is using a lambda unnecessarily.

They probably meant:

for person in people:
    print((lambda x: x[0]   ' '   x[-1])(person.split()))

which performs the .split() once before being indexed.

What this expression:

(lambda x: x.split()[0]   ' '   x.split()[-1])(person)

is doing is creating and executing a lambda in one line. The (person) at the end is calling the lambda with the parameter of person which becomes the x parameter of the lambda.

CodePudding user response:

lambda are function, (person) is calling that function, this should be more clear if we store lambda expression using variable

people = ['Dr. Christopher Brooks', 'Dr. Kevyn Collins-Thompson', 'Dr. VG Vinod Vydiswaran', 'Dr. Daniel Romero']
for person in people:
    print((lambda x: x.split()[0]   ' '   x.split()[-1])(person))

might be rewritten as

people = ['Dr. Christopher Brooks', 'Dr. Kevyn Collins-Thompson', 'Dr. VG Vinod Vydiswaran', 'Dr. Daniel Romero']
func = (lambda x: x.split()[0]   ' '   x.split()[-1])
for person in people:
    print(func(person))

Keep in mind that storing lambdas in variables is discouraged in python, so to make that conform with good style you might change it to

def func(x):
    return x.split()[0]   ' '   x.split()[-1]
people = ['Dr. Christopher Brooks', 'Dr. Kevyn Collins-Thompson', 'Dr. VG Vinod Vydiswaran', 'Dr. Daniel Romero']
for person in people:
    print(func(person))

When you do

for person in people:
    print((lambda person: person.split()[0]   ' '   person.split()[-1]))

you print lambdas themselfes, whilst you are interested in their output values.

CodePudding user response:

lambdas are essentially function. As mentioned in the documentation:

lambda_expr ::=  "lambda" [parameter_list] ":" expression

Lambda expressions (sometimes called lambda forms) are used to create anonymous functions. The expression lambda parameters: expression yields a function object. The unnamed object behaves like a function object defined with:

def <lambda>(parameters):
    return expression

See section Function definitions for the syntax of parameter lists. Note that functions created with lambda expressions cannot contain statements or annotations.

So I suggest you not to use lambda here:

for person in people:
    print(person.split()[0]   ' '   person.split()[-1])

lambdas give instance objects in default, like <function <lambda> at 0x7fab41922a60>.

  • Related