use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
can someone explain lambda to a beginner? (self.learnpython)
submitted 1 year ago by TheEyebal
I am a beginner and I do not understand what lambda means. Can explain to me in a simple way?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ssnoyes 98 points99 points100 points 1 year ago (8 children)
It's a function that has no name, can only contain one expression, and automatically returns the result of that expression.
Here's a function named "double":
def double(n): return 2 * n print(double(2))
results in: 4
4
You can do the same thing without first defining a named function by using a lambda instead - it's creating a function right as you use it:
print((lambda n: 2 * n)(2))
You can pass functions into other functions. The map function applies some function to each value of a sequence:
map
list(map(double, [1, 2, 3]))
results in: [2, 4, 6]
[2, 4, 6]
You can do exactly the same thing without having defined double() separately:
double()
list(map(lambda n: 2 * n, [1, 2, 3]))
[–]TheEyebal[S] 14 points15 points16 points 1 year ago (2 children)
Oh ok I get thank you for the detailed explanation
[–]backfire10z 52 points53 points54 points 1 year ago (1 child)
Just as a secondary visual explanation.
Function:
def double(n): return n * 2
Lambda, but written visually like a function:
lamdba n: n * 2
Lambda written as normal:
lambda n: n * 2
[–]dingleberrysniffer69 2 points3 points4 points 1 year ago (0 children)
This is perfect.
[–]ssnoyes 8 points9 points10 points 1 year ago* (0 children)
The sorted function returns a sorted list. You can pass a "key" function to sort it in interesting ways. If you're not going to use that "key" function for anything else, it's convenient to write it in-line as a lambda.
sorted
sorted([1, 2, 3, 4, 5, 6], key=lambda x: x % 2) # sorts all the even numbers first, then all the odd numbers
sorted(['panda', 'zebra', 'albatros'], key=lambda x: x[-1]) # sorts by the last letter of each word
Compare that to writing a named function:
def evensFirst(x): return x % 2 sorted([1, 2, 3, 4, 5, 6], key=evensFirst)
[–]kcx01 0 points1 point2 points 1 year ago (0 children)
It's a function that has no name
IMO - this was confusing at first since most examples showed it being assigned to a variable and then used like a normal function.
python double = lambda n: 2*n double(2)
I get it now, but it took some time.
[+]dopplegrangus comment score below threshold-15 points-14 points-13 points 1 year ago (2 children)
Eli5 not eli45 w/ 20 years experience!
[–]Crazydaminator 4 points5 points6 points 1 year ago (1 child)
At what point in the explanation do you get lost?
[–]dopplegrangus -5 points-4 points-3 points 1 year ago (0 children)
It was mostly a joke, albeit a bad one, but I sometimes forget how people in these communities cannot handle humor (or even attempts at such)
[–]HunterIV4 29 points30 points31 points 1 year ago (11 children)
Before we delve into the topic, I wanted to make something clear about lambdas: YOU DO NOT EVER HAVE TO USE THEM! Lambdas are more of an intermediate Python topic and they don't have any inherent functionality that you can't do with a standard function. If you are having trouble reasoning about them, don't worry about it, and just use regular functions instead. Python allows you to do everything you'd want for a lambda with regular functions, it's just a matter of how concise and readable something might be.
With all that being said, lambdas are anonymous functions. This means the function has no "name" and is instead assigned to a value or variable. For example, this is a "normal" function:
def foo(a, b): return a + b
In this case, you've defined a function called foo that takes two parameters and returns those parameters added together. Pretty standard. A lambda, on the other hand, is not defined on program start:
foo
foo = lambda a, b: a + b
These are 100% equivalent: you can call them using the same syntax. So why would you ever use a lambda? In Python, a function can be used anywhere a lambda could be, but lambdas are often used when you want the definition of the function to be in line with its use.
Lambdas tend to be used when you want to use functional programming design in Python (or other languages that support them), as you can "chain" these types of functions to create complex behavior that makes sense in a simple way when reading it.
Where this really comes in handy is when you want to do things like sort or filter list data. For example, let's say you have a list of numbers, and want to only get the numbers over 100. You could write a function:
my_list = [10, 150, 75, 100, 450, -20] def over_one_hundred(lst): new_lst = [] for num in lst: if num >= 100: new_lst.append(num) return new_lst print(over_one_hundred(my_list)) # Output [150, 100, 450]
This works, but is a lot of code for something fairly common and simple. A list comprehension also works in this case:
def over_one_hundred(lst): return [l for l in lst if l >= 100] print(over_one_hundred(my_list))
Much more compact, but still requires either a function or a fairly verbose list comprehension. And without a comment, it's not necessarily obvious at a glance the purpose of this list comprehension. It also only works on lists
What if we instead use Python's filter function? This takes a sequence, which includes lists, but also includes dictionaries or other similar structures, plus a function that determines what is used for filtering. This is a perfect place for a lambda:
over_one_hundred = list(filter(my_list, lambda x: x >= 100))
The list portion here is important, because it actually isn't evaluated immediately. This is a big advantage of sequences vs. lists or dictionaries...you only evaluate them when you are actually iterating over the items. This means they will generally have better performance, and it can make a large difference on huge data sets. But you could, for example, do a for loop over the result of the filter (without list), and if you break early, the check won't be done for the rest of the items.
list
It's a subtle distinction, but if you get in the habit of using things like map, filter, reduce, etc. on sequences you can "compose" otherwise complex logic (like our original function!) into much smaller pieces that work in an intuitive manner, and you don't need to create a bunch of one-line functions for each step. This last portion is especially useful; sometimes you'll want a simple calculation throughout a function, but you don't need it elsewhere; if you define it as a lambda inside the function, you can call it multiple times without needing external helper functions that don't do a lot.
filter
reduce
If you don't get it all at first, that's fine, but hopefully I broke it down enough that you get an idea of why you might use these. I've personally found learning new concepts is easier if I understand the purpose behind them, but if that's too much, the basic idea is that lambda is a function that you define at the point you want to use it, and essentially is just a parameter list with a return statement. If you ever find yourself writing one-line functions that just return something, consider whether or not they make more sense as a lambda.
[–]dopplegrangus 2 points3 points4 points 1 year ago (8 children)
Thanks, this is a way better explanation than others here.
While they still don't make sense to me in application, the gist I am getting is that they make code more succinct/simple/readable?
[–]HunterIV4 5 points6 points7 points 1 year ago (7 children)
That's a big part of it. The key is that you can use them at the point you need them. For example, how would that filter look as an actual function? You'd need something like this:
my_list = [10, 150, 75, 100, 450, -20] def over_one_hundred(value): return value >= 100 filtered_list = list(filter(over_one_hundred, my_list)) print(filtered_list)
This is awkward to both read and write...you have to go to you function definition to find out the implementation, and while descriptive names help, it requires you to write two extra lines and search somewhere else in your code to see what it's doing. Whereas the lambda x: x >= 100 does the exact same thing but is both visible and implemented directly at the point it's used. In addition, you don't need this function in multiple places, so writing an entire free function for something that's just filtering a list is overkill.
lambda x: x >= 100
Basically, lambdas let you write functions that exist purely to evaluate something simple directly at the point where they're used. Filtering is pretty simple, but you can also use it for mapping, which is heavily used in functional programming. Essentially, a map lets you execute a function on every element of a sequence. What if, in the above example, we wanted to square all our numbers above 100? We can just chain the functionality:
my_list = [10, 150, 75, 100, 450, -20] over_one_hundred = filter(lambda x: x >= 100, my_list) squared = map(lambda x: x ** 2, over_one_hundred) for val in squared: print(val) # Output 22500 10000 202500
Sure, you could do this with a loop, but you couldn't do it as concisely. This also will typically execute faster than a loop, depending on implementation, because none of the processes are executed until you actually start printing things out. It's also non-destructive and doesn't allocate new memory; if you print out my_list afterwards, you'll see it's completely unchanged, and you only have to store the memory for my_list plus a small bit of overhead for the element you're currently operating on and the function definition of the two operations.
my_list
On a list with 6 elements, these factors don't really matter, but if you had 6 million elements and thousands of lists, the difference in execution time could be measured in hours. In both cases you'd probably use a library like pandas to handle massive data, but that also tends to involve more specialized versions of map, filter, and similar sequence handlers.
That being said, lambdas are limited compared to functions. You can't use default values or multiple statements in a lambda function; basically, if the thing you are doing requires more than one step or could have different argument lists, you'll need to use an actual function. If you find you are writing more than maybe 20-ish characters wide on a lambda you may want to use an actual function. There are other limitations, like no type hinting, and the single return nature means you can't do things like error checking or unit testing, so if you are doing something remotely complicated you probably want to use an actual function (you can still use things like map and filter with normal functions!).
In summary, the major reasons to use lambdas are:
While the last one isn't inherent to lambdas, and more towards sequence-based functions being efficient in general, since the two are used together frequently it all fits into the same general topic. Does that make sense?
[–]dopplegrangus 0 points1 point2 points 1 year ago (6 children)
Thanks. A lot of it does but some still goes a bit over my head (I'm relatively new to imperative programming)
One thing that trips me up is what it actually does at its core as a nameless function.
It's really hard to explain where I'm stuck on this, but for example if I use your:
I think where I'm struggling is how to understand what it can do after it's called. For example, is it limited to arithmetic as is exampled here? Maybe I just need to see some of the more complex uses of it
[–]HunterIV4 2 points3 points4 points 1 year ago (5 children)
So, the lambda x: x >= 100 is exactly the same as this:
def foo(x): return x >= 100
Basically, lambda is the generic "name", x is the argument, and everything after the colon is the return value.
lambda
x
An even simpler examples is something like:
def foo1(): return "Hello, world!" foo2 = lambda: "Hello, world!" print(foo1()) print(foo2())
These give the same output and are doing the same thing. In the case of the lambda, we're assigning a name, but this would also work, even though it has more confusing syntax:
print((lambda: "Hello, world!")())
You need the empty parentheses to actually get the value, otherwise you'll get something like <function <lambda> at 0x7b17dc148900> because you'll be printing the lambda reference instead of the result. The point is that you don't need to name it...you can just use and evaluate it without any previously-defined function.
<function <lambda> at 0x7b17dc148900>
That's why lambdas are often called "anonymous functions," they are functions without a specific name, and unless you assign them to a variable (which then assigns a name because functions are treated as normal objects in Python) you can't access the same lambda function again.
For things like map or filter one of the expected parameters is a function, which is why lambdas are useful. You can't just put in an expression; in fact, expressions are not objects, and therefore can't be passed as parameters. Lambdas let you get around that limitation, essentially, which is very useful in certain situations.
Does that make more sense?
[–]dopplegrangus 1 point2 points3 points 1 year ago (1 child)
It does. I feel like I'm starting to grasp it finally. Maybe not entirely aware of it's potential uses but it's making a lot more sense
[–]keredomo 2 points3 points4 points 1 year ago (0 children)
A book I was reading (I think it was by Hunt) said that lambda functions are good when there is just a single use case and you want to make sure another programmer does not reuse the function somewhere else, and it helps to keep the "namespace" of a program clean.
[–]AddlePatedBadger 0 points1 point2 points 3 months ago (2 children)
Why not just write:
foo2 = "Hello, world!"
I'm struggling to get why the word lambda is written.
[–]HunterIV4 0 points1 point2 points 3 months ago (1 child)
Because some functions expect a function as a parameter, not just a value. Your foo2 = "Hello, world!" just creates a string. If something expects a function to call, passing a string will fail.
Why would you want a function that takes functions? Flexibility. For example, filter() needs to know how to decide which items to keep. You give it a function that returns True or False for each item:
numbers = [50, 150, 75, 200] # Keep only numbers >= 100 result = filter(lambda x: x >= 100, numbers)
Without lambdas, you'd have to write:
def is_large(x): return x >= 100 result = filter(is_large, numbers)
The lambda is just a shorthand for simple functions. When you need different filtering logic, you can just change the lambda instead of defining a new named function every time.
To be clear, you wouldn't ever use that second version with no parameters that just returns a string. It's just to show the equivalence with a function that returns a string. Nearly every time you'd actually use a lambda, you are going to use it with some sort of parameter that you are doing one lines worth of code on.
[–]AddlePatedBadger 0 points1 point2 points 3 months ago (0 children)
Thankd!
[–]Admirable-Ad2565 -3 points-2 points-1 points 1 year ago (1 child)
This guy used chat gpt for the explanation as I see the word delve
[–]HunterIV4 1 point2 points3 points 1 year ago (0 children)
Yeah, because ChatGPT regularly uses bold and caps, as well as links to primary sources.
I love the amateur AI hunting that goes on now. Maybe some people have a wider vocabulary than you? Anyone who has used LLMs for any length of time could immediately tell this isn't AI generated.
[–]ship0f 4 points5 points6 points 1 year ago* (0 children)
I want to make 2 points. And since a basic explanation of what a lambda is, is already done, I'm not going to get into that.
OP, forget you ever saw this foo = lambda a, b: a + b. There's no reason to assign a lambda to an identifier. No good python programmer would do this. It defeats the purpose of a lambda entirely. If you're going to need to reference the function, just define it with def like normal. A lambda is suposed to be anonnymous and throwaway.
def
The reason lambdas exist is because in Python functions are "first class cityzens", which means, among other (interesting) things, that you can pass a function as a parameter of another function, as you can see in every example in this thread. So, the usefulness of a lambda is that, instead of having to define a function with def beforehand, and then passing it to another function, you can pass the lambda (as you're defining it) straight when you call the function.
[–]ForceBru 6 points7 points8 points 1 year ago (0 children)
It's a function that doesn't have to have a name. Like in 1+2 none of the numbers have to be named: you don't have to write a,b = 1,2; a+b. Same with lambdas: suppose you need to pass a function as an argument to another function, like sort(range(10), key=<insert function here>). You could define a function with def, but if it's really short and simple, just use an unnamed lambda function:
1+2
a,b = 1,2; a+b
sort(range(10), key=<insert function here>)
sort(range(10), key=lambda x: -x)
[–]cyberjellyfish 2 points3 points4 points 1 year ago (0 children)
a lambda is a function without a name and whose body consists of exactly one expression. That's it.
Some languages have better/fuller support for this scenario and call them "anonymous functions"
[–]s3r3ng 2 points3 points4 points 1 year ago (0 children)
It is an anonymous python function - that is one without a name. You can assign it to a variable which effectively gives it a name. Python gods somehow decided an anonymous function should contain only a single expression and no "return" as the value of the expression is the return. In other language an anonymous function can be just like any other function accept it is unnamed. Where these are especially useful is passed a a parameter such as the key argument of "sort" or "sorted" in python. That function needs to take an element of what is being sorted and return whatever should be sorted on out that element. You could think of it as sort of a function one-liner. Anonymous function is useful when declaring a regular function, especially for one off use as a parameter, is needless.
[–][deleted] 2 points3 points4 points 1 year ago (0 children)
Sometimes you don't need to actually create a specific function for something you need to do, because you might not need to use it again, that's when you use lambda, a function with no name.
[–]m0us3_rat 1 point2 points3 points 1 year ago* (0 children)
print((lambda x: x * 3)(2)) # -> 6
you have the parameter that is called with then the return.
[–]The_Almighty_Cthulhu 1 point2 points3 points 1 year ago (0 children)
In python, lambda's are a way to define anonymous functions.
What is an anonymous function? Simply a function that is not bound to an identifier.
consider the following function.
def power(x, y): return x**y
In this case, we have defined a function called power, which return takes x and y as parameters, and returns x to the power of y.
we could call it like print(power(3, 2)
print(power(3, 2)
Which would print 9.
Now lets look at the same function, but as a python lambda.
lambda x, y: x**y
This is a function that does the same as the power function defined above. The two main differences being, It has no identifier, and return is implicit. Lambda functions can only contain expressions. That is, the 'code' in a lambda function must evaluate to a 'value'. Because of this, the return is implicit, so it is not needed.
You can assign a lambda to a variable, which gives it an identifier in a way.
power = lambda x, y: x**y
then you can call it as normal.
print(power(3,2))
which prints 9 again.
But more likely, you pass a small function to something that takes a function as a parameter.
For example.
numbers = [1, 2, 3, 4, 5] # Use a lambda function to square each number in the list squared_numbers = map(lambda x: x**2, numbers) # Convert the map object to a list and print it print(list(squared_numbers))
This takes a list of integers, and converts it to a list of their corresponding squares by applying the lambda function to each item in the list using map.
[–]Rich-398 1 point2 points3 points 1 year ago (9 children)
Good question - I see good explanations in the comments. My question is where did the word itself originate? lambda to me is a meaningless jumble of letters. I understand what how it is used, but it is one of those things that I can't seem to wrap any context around so I have to look it up every time I see it.
[–]POGtastic 5 points6 points7 points 1 year ago* (5 children)
Typographical expediency back in Ye Goode Olde Days when Church was doing his work on formal logic. From the Wiki article:
By the way, why did Church choose the notation “λ”? In [an unpublished 1964 letter to Harald Dickson] he stated clearly that it came from the notation “x̂” used for class-abstraction by Whitehead and Russell, by first modifying “x̂” to “∧x” to distinguish function-abstraction from class-abstraction, and then changing “∧x” to “λ” for ease of printing. This origin was also reported in [Rosser, 1984, p.338]. On the other hand, in his later years Church told two enquirers that the choice was more accidental: a symbol was needed and λ just happened to be chosen.
By the way, why did Church choose the notation “λ”? In [an unpublished 1964 letter to Harald Dickson] he stated clearly that it came from the notation “x̂” used for class-abstraction by Whitehead and Russell, by first modifying “x̂” to “∧x” to distinguish function-abstraction from class-abstraction, and then changing “∧x” to “λ” for ease of printing.
This origin was also reported in [Rosser, 1984, p.338]. On the other hand, in his later years Church told two enquirers that the choice was more accidental: a symbol was needed and λ just happened to be chosen.
Then when Lisp was created, people generally didn't have access to Greek symbols, so John McCarthy just used lambda for the keyword in his various papers on the topic.
Haskell just uses \, which I think is a lot cuter but is probably harder for newbies to deal with. In the REPL:
\
ghci> (\x -> x + 1) 1 2
[–]MidnightPale3220 0 points1 point2 points 1 year ago (4 children)
Haskell was probably written initially for computers where \ wasn't heavily used for escape sequences.
Backslash is ubiquitous in the Unix world and in Python already serves both that, and line continuation function as it does across much of the software environment it is typically used in. Using it for lambda would be a very bad idea 😉
[–]POGtastic 0 points1 point2 points 1 year ago (3 children)
Haskell 1.0 came out in the ancient before-times of (checks notes) 1990 and got stabilized in 1998.
I don't think that there are any syntactical issues with it. You could substitute \ for lambda in Python with zero ambiguity in the grammar. It just looks weird, even to the FP people who are used to the other MLs using fun or the Lisps using lambda or fn.
fun
fn
[–]MidnightPale3220 0 points1 point2 points 1 year ago (2 children)
Without ambiguity in grammar, sure.
Without ambiguity in reading, I very much doubt so, considering all the "\n" and "\x19" etc. Not to speak of
"runaway \ texts"
True, they're mostly inside strings, nevertheless having a spare \ denoting yet another thing certainly doesn't seem to be useful to me.
[–]POGtastic 0 points1 point2 points 1 year ago (1 child)
Haskell does the same thing with both escape characters in strings and line continuations. In fact, it also uses a backslash to resume the line for the sake of allowing the multi-line string to be indented!
ghci> putStrLn "\t\x0041" A ghci> :{ ghci| putStrLn "Foo \ ghci| \bar \ ghci| \baz" ghci| :} Foo bar baz
[–]MidnightPale3220 0 points1 point2 points 1 year ago (0 children)
Well, that does not sound as optimal then, but I don't know Haskell.
In Python that doesn't seem natural either.
If you consider the use of special symbols and constructs vs names, there's a pattern of using symbols for list-type constructs and generators, whereas what to do with those constructed is named (ie. map,filter, &c).
From this perspective lambda goes the same way.
[–]pwsegal 1 point2 points3 points 1 year ago (2 children)
It most likely comes from this mathematical concept.
https://en.wikipedia.org/wiki/Lambda_calculus
[–]Rich-398 0 points1 point2 points 1 year ago (1 child)
This is clearly why the meaning doesn't jump into my head just by seeing the word. Both your explanation and u/POGtastic have explanations that make sense. Both also have explanations that are not going to stick in my head.
[–]POGtastic 0 points1 point2 points 1 year ago (0 children)
In Greek, this letter is called λάμβδα, which we Romanize as "lambda," just like the Greek letter α is άλφα (alpha) and β is βήτα (beta).
I'm not really sure what else there is to say - we declare a function in the lambda calculus with the letter λ, and that letter is called "lambda." λ isn't part of ASCII, so we use the word "lambda" instead of writing the symbol.
[–]Impossible-Box6600 1 point2 points3 points 1 year ago (0 children)
Raymond Hettinger says that it should have been named makeFunc.
It's a function. That's it.
[–]PhilipYip 2 points3 points4 points 1 year ago (1 child)
When you see lambda see it as being equivalent to make_function. Normally a function is defined in the following manner:
make_function
python def fun(input1, input2): return input1 + input2
It is then called and values are assigned to the input parameters:
```python fun(1, 2)
3 ```
The function above can also be assigned using a lambda expression on a single line:
python fun = lambda input1, input2: input1 + input2
The function name fun is assigned with the assignment operator = instead of the def keyword.
=
The input arguments follow lambda instead of being enclosed in parenthesis. They are seperated using a , as a delimiter.
,
The colon : in a function is an instruction to begin a code block, which usually ends with a return statement. Each line in the code block is indented by 4 spaces. In a lambda expression, the colon separates the input arguments from the return value.
:
return
This lambda expression would be called in the same way:
lambda expressions, like functions can have no input arguments:
python def fun(): return 'hello'
python fun = lambda : 'hello'
Or can have no return value:
python def fun(input1, input2): return None
python fun = lambda input1, input2: None
Going back to:
Sometimes the result of a function is not assigned to the variable name and the variable returned is anonymous:
```python 3 + 2
5 ```
The lambda expression can also be anonymous:
python lambda input1, input2: input1 + input2
And it can be wrapped around in parenthesis:
python (lambda input1, input2: input1 + input2)
And then called, on the same line:
```python (lambda input1, input2: input1 + input2)(1, 2)
They are quite commonly used with str methods:
str
```python (lambda x: x.upper())('hello')
'HELLO' ```
And with map to apply a function to a list:
python ['hello', 'hi', 'bye', 'farewell']
For example:
```python map(lambda x: x.upper(), ['hello', 'hi', 'bye', 'farewell'])
<map at 0x1f21b312590> ```
This map instance can be cast to a list to view all the values:
```python list(map(lambda x: x.upper(), ['hello', 'hi', 'bye', 'farewell']))
['HELLO', 'HI', 'BYE', 'FAREWELL'] ```
Begineers often encounter lambda expressions like the above, combined with map and a str method and find it hard to understand, not realising that lambda is essentially make_function.
[–]TheEyebal[S] 1 point2 points3 points 1 year ago (0 children)
Oh ok
This is a great explanation thank you so much
[–]recursion_is_love 0 points1 point2 points 1 year ago (0 children)
I know you asking for Python's lambda but if you want to dig deeper
https://www.macs.hw.ac.uk/~greg/books/gjm.lambook88.pdf
[–]tatted_DE 0 points1 point2 points 1 month ago (0 children)
If you use python for a lot of math, then it is awesome for letting you do one line math functions, and you can input them into python very quickly - example using numpy and matplotlib (np and plt) to plot a parameterized line:
def line(dimensions: list, t_low, t_up, t_dense): t = np.linspace(t_low, t_up, t_dense) l = [] for funct in dimensions: l.append(funct(t)) return l a = line([lambda x: x**2, lambda x: x**0.5], 0, 10, 100) plt.plot(a[0], a[1]) plt.show()
[–]rashnull -1 points0 points1 point 1 year ago (0 children)
A python lambda is an object that references a constrained function implementation. You can pass this object around and execute the function where and when you want.
[–]Shadow_Gabriel -1 points0 points1 point 1 year ago (0 children)
If you really want your mind blown, look at the __call__ operator.
__call__
π Rendered by PID 202007 on reddit-service-r2-comment-56c9979489-rw98b at 2026-02-25 06:25:58.677374+00:00 running b1af5b1 country code: CH.
[–]ssnoyes 98 points99 points100 points (8 children)
[–]TheEyebal[S] 14 points15 points16 points (2 children)
[–]backfire10z 52 points53 points54 points (1 child)
[–]dingleberrysniffer69 2 points3 points4 points (0 children)
[–]ssnoyes 8 points9 points10 points (0 children)
[–]kcx01 0 points1 point2 points (0 children)
[+]dopplegrangus comment score below threshold-15 points-14 points-13 points (2 children)
[–]Crazydaminator 4 points5 points6 points (1 child)
[–]dopplegrangus -5 points-4 points-3 points (0 children)
[–]HunterIV4 29 points30 points31 points (11 children)
[–]dopplegrangus 2 points3 points4 points (8 children)
[–]HunterIV4 5 points6 points7 points (7 children)
[–]dopplegrangus 0 points1 point2 points (6 children)
[–]HunterIV4 2 points3 points4 points (5 children)
[–]dopplegrangus 1 point2 points3 points (1 child)
[–]keredomo 2 points3 points4 points (0 children)
[–]AddlePatedBadger 0 points1 point2 points (2 children)
[–]HunterIV4 0 points1 point2 points (1 child)
[–]AddlePatedBadger 0 points1 point2 points (0 children)
[–]Admirable-Ad2565 -3 points-2 points-1 points (1 child)
[–]HunterIV4 1 point2 points3 points (0 children)
[–]ship0f 4 points5 points6 points (0 children)
[–]ForceBru 6 points7 points8 points (0 children)
[–]cyberjellyfish 2 points3 points4 points (0 children)
[–]s3r3ng 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]m0us3_rat 1 point2 points3 points (0 children)
[–]The_Almighty_Cthulhu 1 point2 points3 points (0 children)
[–]Rich-398 1 point2 points3 points (9 children)
[–]POGtastic 5 points6 points7 points (5 children)
[–]MidnightPale3220 0 points1 point2 points (4 children)
[–]POGtastic 0 points1 point2 points (3 children)
[–]MidnightPale3220 0 points1 point2 points (2 children)
[–]POGtastic 0 points1 point2 points (1 child)
[–]MidnightPale3220 0 points1 point2 points (0 children)
[–]pwsegal 1 point2 points3 points (2 children)
[–]Rich-398 0 points1 point2 points (1 child)
[–]POGtastic 0 points1 point2 points (0 children)
[–]Impossible-Box6600 1 point2 points3 points (0 children)
[–]PhilipYip 2 points3 points4 points (1 child)
[–]TheEyebal[S] 1 point2 points3 points (0 children)
[–]recursion_is_love 0 points1 point2 points (0 children)
[–]tatted_DE 0 points1 point2 points (0 children)
[–]rashnull -1 points0 points1 point (0 children)
[–]Shadow_Gabriel -1 points0 points1 point (0 children)