Functions as First-Class Objects in Python
What does “Functions as First Class Objects” mean? You’ll see in this lesson that it means that functions can be passed around into lists and used as arguments for other functions. In the next lesson you’ll learn how to define functions inside functions.
00:00 When I say that functions are first-class objects, I mean that they can be used as arguments also, just like any other object—a string, an integer, a float, a list, and so forth. Let me have you dive into that.
Let’s make a couple quick functions and see what can be done. These are two printing functions that are slightly different. The first one,
say_hello(), is going to use an f-string and return it saying
f"Hello " with curly braces around the argument. To call the function,
say_hello(), and then put in a
Can one function literally call another function? Let’s try it out. For the next function—I’ll have you call it
greet_bob() takes a function, some form of a greeter function, as its argument.
So instead of a name it’s saying, “Well, what function do you want?” and then it returns that greeter function with this name,
"Bob". So it’s going to plug
"Bob" into whatever greeter function as an argument.
So, how would you use it?
greet_bob itself is a function, so to use
greet_bob(), you enter in the function you want. You have two to choose from.
say_hello—you don’t need to put an argument here, it’s going to get it here.
Become a Member to join the conversation.