 In this video, we are briefly discussing how we can create functions without a name, which we then would refer to as anonymous functions. So let's first create a new file and rename it into anonymous.ipython. Well, first of all, you may wonder why would I ever want to do that? Why would I ever want to create a function that has no name? Because if a function doesn't have a name, how do we call it? Well, the answer to that is that we will see in future videos that there are applications where it is necessary to put in a function, for example, or a reference to a function as an input to some function that then does something with the function. For example, apply it to every element in the list or something like that. So whenever we have a situation like that, for syntactical reasons, we need to create a function object. However, the whole purpose of having functions is, as we have discussed before, to reuse it. That's the whole point of having functions. So whenever we are in a situation where we need a function from a syntactical point of view but we don't want to reuse it, then it pays off maybe to simply create a function object without a name. Let's start with a brief example. So let's create a simple function that simply adds three to whatever number we give it as the only argument. So what would such a function look like? For example, like this, let's define it and let's call it add three. And the function, as I said, takes one argument. Let's call it simply num for number. So we also provide a doc string and when we say add three to the only argument passed in. So that is quite nice. And then we simply go ahead and write return num plus three. So that's quite easy. So let's define the function and let's use it. So add three. Let's for example give it the argument one and of course we get back four. So let's see in memory what this means. So you could think of the depth statement as follows. At first, the depth statement will go ahead and create a function object somewhere on the right hand side in the memory where all the objects are. And it will put some code in there. So in this case, just num plus three. And we will add a type to that and the type is function. And then we will give the function a name just like any other variable. So let's call it add three and make this variable reference the function object. Okay. So this is what happens when we define a function using the depth statement. So again, two things. First create the object and then basically assign a variable name to that object. However, these are two different things. So as we have seen before in different contexts, we may create an object and not assign it to a variable name. For example, when we exchange an element in list, for example, because then we simply go ahead, create a new object and make a list point to that but don't create a new variable. Okay. So really what's going on here is there are two different things going on that using the depth statement will always happen together. So wouldn't it be nice if we could just create the function object without creating a name for that. And of course, we can do that. So the syntax will look like this. And in the beginning, it may look a little bit weird, but it is actually not so hard. So the syntax is we are going to use the so-called lumpter statement, lumpter within B, of course. So lumpter and lumpter is a keyword in the Python language, which creates a function object and it's an expression. So it is not a statement really. So in other words, as we learned in a previous video, an expression basically means it evaluates into some object and then it is up to us what we do with the reference to the object. So here we are going to get back a reference to a function object and it depends on us what we do. So let's continue here with the syntax. So first, the lumpter, the lumpter expression receives all the parameters that the function should take. So for example, num, and then we simply go ahead and write colon. So there is no parentheses here. And then on the right-hand side of the colon, we have to write one expression only. So that is a limitation for lumpter expressions. The function we create can only consist of one expression. So one of the functions we saw before, the average event functions, had two expressions, two lines of codes in it, so we could not rewrite this function using a lumpter expression. However for easy functions just like the one we see here, this works. So let's go ahead and simply write num plus three. And also note how I do not write the return statement here. So the return statement is the thing that returns some value after the function is done. However, the lumpter expression does not take it. And that's it. That's the example of a lumpter expression. So when I execute the cell, I get back, as we see below the cell, a function object which has no name really. So it's kind of anonymous. And what that means in memory is basically when we evaluate the cell, what's going to happen is we are going to create an object with the same code in it as the other function object. This object is also of type function. And then what happens is, and I show that as I did in a couple of previous videos before with a red dotted line, I'm getting back a reference to this object. However, we are not doing anything with it. And that is why I see here, and that's why I use red. That's why I see here the reference to the function object. Okay, so I'm not doing anything. Now what have we learned before? What happens if I don't do anything with the reference? What's going to happen is nothing is going to happen. So we just simply don't have a reference to this object. And that means after some time, Python's garbage collector comes in and simply removes the object. So let's go back here in a notebook. So in other words, every time I execute the cell, I create a new function object. I don't give it a name. And because there is no reference to the object, the object will eventually be garbage collected. So now you may wonder, what can you do with it? Well, technically speaking, what you could do is the following. You could assign this object, this expression, which evaluates into an object. You could assign this object to a variable. Let's call it add three alt for alternative, just like this. And now what we could do is I could actually go ahead and call the variable because as we learned, any variable in Python always works the same way. So the variable add three and the variable add three alt are basically working in the very same way. And now if I go ahead and call this function object, I also get back four. However, this is kind of something that you don't see in a code base because whenever we want to give a function name, we would simply use the def statement. Okay. So in other words, that is something we rarely ever see. So usually the left-hand side, it works as we see, but we don't do that. So what else could we do? So what we could do is at least for now, we could not only create the object, but we could also call it right away. So how we do that is as follows, I will first go ahead and put a couple of parentheses around the lambda expression that is there because otherwise what's going to follow will not work. And then, so this is basically just a grouping parentheses. So this is basically grouping this together. And then I'm going to add another pair of parentheses. And the second pair of parentheses is now the call operator that executes the function. And because the function takes, as we see one argument, that means if I execute the cell right like this, I will get a type error and it says, well, we are missing a required argument here, num. So in other words, let's simply go ahead and give it an argument. Let's, for example, give it the argument one, and then I get back four. So what I do in the second cell is I create a new function object. I immediately call it with the one as the argument, and I get back a return value. And then after I call the function, the function because it still has not a reference to it will immediately be garbage collected, it will immediately be gone. Okay, so that is at the moment, this is rather pointless to do because we are essentially creating a function calling it and then forgetting about the function. So this does not make a whole lot of sense. However, there will be in future videos, there will be applications where we need to pass in a function to some other function. And then the other function does something with the first function. And in this situation, we could pass in either a reference to a function which we defined with the dev statement, or simply we could simply pass in a lambda expression. Okay, so that is how we can create functions that don't have a name to it. But again, it's the same function object as as above, it just works like as above. And then often what you see is, when people use lambda expressions in real life, they also similarly to list comprehensions, try to keep the line, the code cell as compact as possible, which is why instead of saying num, we often see simply x. So in other words, the function takes an argument that's called it x and it returns x plus three. Okay, so oftentimes, this is what we see here. Okay, so that is how we create a function without giving a name to it. So in other words, this is how we only get half the job done here, only create the functional object without the name. So we can split up these two processes basically that otherwise would always go together into two different steps. Okay, that is it for this video. So I see you soon.