Python Lambda Functions Hurt My Head

Learn to code, they said. It's important.

Learn to code, they said. You're good at logic.

Learn to code, they said. It's just like algebra.

Sure. Code is important and usually logical and follows a lot of math rules. But there's a few things that keep tripping me up because they seem to break some of the conventions of algebra.

Lambda Functions in Python are one of those tricky things that I really struggle to consistently wrap my head around. They should be so easy! They're literally disposable! And they look straightforward, since they are usually quite short.

But for some reason I am always getting stuck writing them, because I feel like they have one too many undefined variables. I feel like I'm trying to solve a system of equations with two variables, but somebody only gave me one of the equations. Somehow I balk at this every time and think I haven't possibly provided enough information for the function to properly run. I'm racking my brain adding elements that aren't necessary.

Even in a lambda function with only one variable, it feels like one too many!


But how does it do all that computing without me telling it all the numbers???? (Source: https://xkcd.com/2453/)

Let's take a little sample here:


So the "x" stands in for whatever value we'll use when we next call this function. I get that. So then what the heck is that other variable doing in there? It's too many undefined variables! My middle school math teacher is mad! I am mad! But somehow, Python is not angry at all.

The reason this works has a lot to do with the context you would typically use a lambda function in. You are likely to find a lambda function helpful in situations where you have other stuff going on simultaneously. A lambda function cleans up code that has multiple steps. So that other variable is probably coming from somewhere else outside the lambda function. Let's zoom out here:

So here, the lambda function is essentially nested inside another bit of the code, and that's where it picks up a definition for its variable. The thing that confuses me every time is that the variable gets defined AFTER you write the lambda function.

I suspect that lambda functions will be hard for me until they suddenly aren't, like so much of coding. But I do have a strategy for dealing with them, a way to work with my brain instead of against it.

Begin at the end


I simply go backwards.

The computer reads the code in order, but I certainly don't need to write it that way. By going backwards, by the time I get to the lambda function, everything is defined and my brain is not sending up unnecessary red flags. Working backwards, I know what the x is supposed to be and that helps me both stay calm and also write the lambda function properly. (This also works for list comprehensions, another Python time saver that messes with my mind.)

For the example above, I think, "I need to multiply two numbers, and each of those numbers might change. I insert one of the numbers at the end, in "what_now", and another one earlier on in "seems_undefined". That's two spots accounted for, so I can have two variables in my lambda function.

It still looks funky to me, but by looking ahead, I know it's going to work because those variables are going to turn in to actual numbers.

I am not a Python processor, so my brain sometimes approaches things in a different order than the program. It's helpful to realize where those differences are so I can find workarounds to help me make sense of what I'm trying to code.


I hate lambda functions slightly less now, but I must be honest. I'll probably still define a function instead if I can get away with it.

But when I have to do a lambda function, there will hopefully be a fraction less frustrated keyboard mashing.






Comments

Popular posts from this blog

Using Data Science to Help Kids Graduate

Back to Binary with OneHotEncoder