A Guide to Python Lambda Functions (adamj.eu)
from learnbyexample@programming.dev to python@programming.dev on 29 Apr 2024 15:32
https://programming.dev/post/13419407

#python

threaded - newest

tunetardis@lemmy.ca on 29 Apr 2024 16:00 next collapse

They’re somewhat more capable now that we have the walrus (:=) operator.

BeefPiano@lemmy.world on 29 Apr 2024 20:51 collapse

Can you give an example? Can you use it to initialize vars outside the scope of the lambda?

sugar_in_your_tea@sh.itjust.works on 29 Apr 2024 22:05 next collapse

The general form is:

(var := expression) rest of condition

So you can do something awful like this:

lambda: (y := 1) != 2 and y

Not sure if that’s “good,” but it does kind of let you sneak a statement into the lambda.

tunetardis@lemmy.ca on 29 Apr 2024 22:41 collapse

Can you use it to initialize vars outside the scope of the lambda?

No, that’s not what it’s for. It lets you define a temporary local variable within an expression. This is useful in situations where you might want to use the same value more than once within the expression. In a regular function, you would just define a variable first and then use it as many times as you want. But until the walrus operator came along, you couldn’t define a variable within a lambda expression.

Can you give an example?

Ok, I’m trying to think of a simple example. Let’s say you had a database that maps student IDs to records contain their names. To keep things simple, I’ll just make it plain old dict. And then you have a list of student IDs. You want to sort these IDs using the student names in the form “last, first” as the key. So you could go:

>>> student_recs = {1261456: {"first": "Harry", "last": "Potter"}, 532153: {"first": "Ron", "last": "Weasley"}, 632453: {"first": "Hermione", "last": "Granger"}}
>>> student_ids = [1261456, 532153, 632453]
>>> sorted(student_ids, key = lambda i: (rec := student_recs[i])['last'] + ', ' +  rec['first'])
[632453, 1261456, 532153]

The problem here is that student_ids doesn’t contain the student names. You need use the ID to look up the record that contains those. So let’s say the first ID i is 1261456. That would mean:

rec := student_recs[i]

evaluates to:

{"first": "Harry", "last": "Potter"}

Then we are effectively going:

 rec['last'] + ', ' + rec['first']

which should give us:

 'Potter, Harry'

Without the := you would either have to perform 2 student_recs[i] look-ups to get each name which would be wasteful or replace the lambda with a regular function where you can write rec = student_recs[i] on its own line and then use it.

Am I making any sense?

tunetardis@lemmy.ca on 29 Apr 2024 22:58 collapse

Actually, now that I think of it, there’s no reason you need to join the 2 names into a single str. You could just leave it as a tuple of last, first and Python will know what to do in comparing them.

>>> sorted(student_ids, key = lambda i: ((rec := student_recs[i])['last'], rec['first']))
[632453, 1261456, 532153]

So the lambda would be returning (‘Potter’, ‘Harry’) rather than ‘Potter, Harry’. But whatever. The := part is still the same.

driving_crooner@lemmy.eco.br on 29 Apr 2024 16:12 next collapse

I use lambda functions on my panda’s data frames when I need to do row operations, with .apply().

lascapi@jlai.lu on 30 Apr 2024 07:51 next collapse

I discover the operator module! Amazing! 🤩

The operator Module A third alternative to writing lambda functions is to use the standard library’s operator module. This module contains some predefined functions and function factories, which can replace the most common use cases for lambda functions. Let’s look at both of these separtaely, factories first. Another note: the function that attrgetter returns is implemented in C, so it’s slightly faster than using either a normal or lambda function.

Useful if you want to speed up your code.

logging_strict@programming.dev on 02 May 2024 10:17 collapse

BFF 1: Chuck is going to be so surprised. We’ve hired the best caterers, cosplay theme, fireworks, yard ales, twister themed blind laser tag, there will be balloons, ball pool, athletic challenge course, fabulous entertainment from top notch talent, show girls, the wine will flow like water

BFF 2: Tell me you didn’t invite that grifter, mypy

BFF 1: I … err … hum. Opps

mypy enters the conversation

Don’t use lambda use def function instead

BFF 2: Man i hate that guy. Rains on our parade and sucks the fun out of the room (and this entire thread).