Transcript for:
Understanding First-Class Functions in Python

While not precisely the same as other types in Python, for example, ints and strings and lists and so on, functions are considered first-class citizens in the language. This means that functions can be assigned to variables, returned from other functions, and stored in data structures like lists or tuples. This is most often done by using the function name without any parentheses. For example, here we are assigning some function, which is presumably some function that we've written to a variable named var. A variable or parameter that refers to a function can be used to call that function. Any required arguments must be passed in when the function is called, just as you'd expect. The return values can also be used. In this example, we're calling the function that is referred to by our variable named var. Remember that this is some function. We are passing the arguments foo and bar into the function call and then whatever the return value is is saved in the variable x this also means that we can write functions that declare function parameters a function is passed in as an argument to one of the function parameters the function parameter may be called from inside of the other function a function may declare one or more function parameters and call any functions that are passed in as arguments in this example the sheep function declares a parameter named sound function. On line 2, we are calling sound function with no arguments and storing the return value in the sound variable and then printing it out on line 3. This means that we can write one function that changes its behavior depending on which other functions are passed in as arguments. For example, on line 14, we're calling the sheep function and as an argument we are passing in the bah function. Notice we're using the name of the ba function without any parentheses. Then, inside of the sheep function, it calls the ba function, which then returns the string ba, and we then print it out on line 3. Let's take a closer look at this example. Let's start as we so often do by messing around in the Python interpreter. To start, I'm going to assign a function to a variable. In this case, I have assigned the print function to the variable x. If I take a look at the value of x, it tells me that it is the built-in function print. If I print it out, it prints the same thing. Now, if I want to use x to call the function, I can put parentheses after the name of x, and then whatever value it is that I want to be printed to standard output, and then I can hit enter, and it prints out. So x is effectively an alias for the print function now. I can do the same thing with the input function. Remember, the input function also requires parameters, the prompt that we want to prompt the user with, but it also returns a value. So let me assign the variable z to the return value that I get from calling y. We can see that that return value has been stored inside the variable z. Now, using what we've learned, let's write a program. I'm going to recreate the example from the lecture, first by writing the three functions that return sheep sounds. Next, I'm going to write that sheep function that takes some other function as a parameter. Ideally, this will be one of those functions that returns a sound, so we're going to store that in a variable. Note that the sound function parameter has no parentheses, but when I call the function, I'm using parentheses, just like when I call any other function. Next, I'm going to write a main function to test my sheep function with each of the three sound functions that I wrote previously. Now let's see what happens when I run the function. We can see that each time I call the sheep function on lines 15, 16, and 17, I'm passing a different one of my sound functions in as a parameter. So first I'm using ba and we can see that when it prints out it says the sheep says ba. Next I'm I'm calling it with the RAM function, and we can see when it prints out, it says the sheep says RAM. Finally, I'm calling it with the U function, and we can see it print the sheep says U. So let's push it a little bit farther by making one more function. Without making any changes to the sheet function, I'm going to call it a fourth time with the new hi there function and see what happens. It works just as well as it did with the other three functions. In fact, given any function that returns any value suitable for printing, the sheep function will always print the sheep says and then whatever its function parameter returns. By writing functions that declare function parameters, we can alter the behavior of the function simply by changing the function that we pass into it. In this example, we have one function that ends up printing four different things depending on the function parameter that is passed in.