From the course: Python Essential Training

Functions as variables - Python Tutorial

From the course: Python Essential Training

Functions as variables

- [Instructor] Variables have a name and some data associated with them. Functions have a function name and some data associated with them. Okay, in the case of a function, this data is information about the required parameters, if any, and also some lines of instruction need to be executed. And a function is actually represented in Python as an object. And we can see this using the underscore underscore code attribute of Python function objects. Okay, so if I print out co, varnames, let's do print in here and then print co_code. Okay, so here are the variable names. In this case we don't have any. And also a Python byte object of all of the lines of instruction in this function. Now, you'll probably never to actually need to use this. I actually looked it up specifically for this lesson so please don't take notes on underscore underscore code. You're not going to need it. But what you do need to know is that functions are not anything special in Python. They are just variables associated with some data. And to demonstrate this here's some text that you will probably recognize from earlier, and also some function names. And these two various text processing operations. So we have function that makes the text all lowercase, removes punctuations, removes any new line characters represented with that backslash n, removes words if the the word length is three or less, and then also removes the long words. Now I can easily mix and match the order I perform these functions in and which functions I apply by calling them in a list. So let's make a list called processing functions, okay? And add lowercase, remove punctuation, remove new lines. Okay, for func in processing functions, text is equal to func text, whoops. Print text. And you can see that I can easily add functions here like remove long words. Now we have all the short words. So you can imagine how powerful this pattern can be for many business processes that suffer from, let's just say, frequently changing requirements. And finally, one last way to represent a function that you might use and encounter. So not every piece of data needs a variable name, right? Like I can just type five and that kind of exists. I can do two plus three, no variable name required. Well, you can do the same with functions as well and they're called lambda functions like the Greek letter lambda. And you can define a small function without a variable name like this, lambda x, and then a colon, x plus three. And we can call it then with five and that returns eight. So you type lambda, followed by your parameter names. In this case, just that single parameter X, then a colon, and then a one line function. Sorry, no multiline functions with lambda functions. You don't need to type return. The return is automatically assumed. There's an implied return, as we call it. And so this takes the value five, adds three, and returns eight. And sometimes this comes in handy in some Python functions that take in a function as their argument. For instance, for sorting a list of values you might use the function sorted. So here's my list and it's five, four, three, two. Great, sorted, my list. And you can see it sorts those values. But if the things you're sorting don't have an obvious numeric value, you can pass in a function that takes an item in the list and returns the value that Python should be using to sort it. Okay, for example, my list, it's going to be a list of dictionaries now, num three, num two, and num one. Okay, now we want to call this sorted function again. My list, and you can see that we can't sort it. But if we pass in this key parameter, which is a function, lambda x, x is going to be our item in here, and then return x number, and now it can sort it. A lambda function is really a convenient and concise way to write little mini functions that you just need while you're writing your code. And that's it, you are now a functionally competent programmer.

Contents