The anatomy of a function
- We've been working with functions throughout this course. I mean, literally the first Python program we wrote used the print function to print Hello World. So many introductory programming courses use the recipe metaphor for programming. A program is like a cake recipe. Follow the step-by-step instructions one at a time and you'll get your cake. And sure, at some level, programs are executed step by step, by a process, reading lines of instruction. But programs are really so much more than that. It's not a flat, linear, start here and end here set of instructions. The programmer must think in terms of systems, of tasks, of objects and interacting components. In terms of designing and thinking about programs, the basic unit of a program is not a line of instruction, but a function, and a program without any functions is, well, functionless. So let's take a closer look at this basic and vital unit of programming, the function. Let's go to the code. As we all know, functions have a name and some parameters that are indicated using the def statement. So let's make a function. performOperation, num1, num2, and operation. So let's make a simple function that takes in two numbers and an operation, either the string sum or the string multiply, and returns a result. So if the operation is equal to sum, we're going to return num1 plus num2. If operation equals multiply, I return num1 times num2. And we can call it performOperation, two three, and sum. And we get five. So let's say that most of the time we want to add the parameters together, but we don't want to have to write sum all of the time. We can provide a default value using what's called name parameters or keyword arguments. So let's bring this down here and let's say the operation, let's give it a default value of sum. Now we can take this away and we get our five. Of course we can also provide our own value. Operation equals multiply, and that overrides it. Now you don't actually have to say operation equals multiply when you're calling this function. You can also just pass multiply in as the third parameter. But if you have a function with lots and lots of these optional keyword parameters, you don't want to have to worry about what order you're passing everything in. So sometimes it's easier, more readable to say operation equals multiply. For example, we can add another keyword argument to our function here. Let's say we have a message, and there's some default message and then that message gets printed out. Okay. So when we call this function, we can pass message before or after the operation. A new message. Add a comma in there. As long as we're defining which argument is which. One rule however, the keyword arguments have to come after the what's called positional arguments, this two and three. The first two arguments, this order matters very much. They're not optional. But then after them the keyboard arguments can be in any order you want. So these optional arguments are nice, but there's still this sort of fundamental limitation or functional limitation on what we can do here. We need to anticipate all of the variables that the user might be passing in. What if we want to allow them to pass in any number of variables they want? Sounds crazy, right? Well, let's do this. performOperation args, print args. performOperation one, two, three. Well, what did you expect? There's a syntax error. The way I written this function, I'm telling it that it's supposed to be expecting a single variable called args but there are three of them I'm passing in. So there's a mismatch. But watch this, as you might guess from the title there. Nice. This asterisk right here on the args tells Python the variable name isn't literally args. This is just a reference, a pointer to the stuff that's being passed in. Technically, you don't have to use the word args here as this variable name, but you should use args because it's sort of the Python standard. And of course when we call this we get our old friend the tuple. Now this trick only works for positional arguments, not keyword arguments. So if I pass this in, let's actually do this down here. So if I pass this in, I still get an unexpected keyword argument, okay? And to handle keyword arguments, we need to do this differently with something called kwargs, okay? Which stands for keyword arguments. Now, if we take this, and let's also print kwargs. There we go. Notice that the keyword arguments are a dictionary instead of a tuple. And this makes sense because keyword arguments can have keys and values. They can also be passed in any order so you need a dictionary to reference them. And now for the piece de resistance, let's rewrite our performOperation function for ultimate flexibility. And to do this, we're going to need a little help from the math library, import math. Okay, so let's take this and rewrite this. I'll just use args. We do want to keep operation equals sum by default. If operation equals sum, return math.sum args. If operation equals multiply, return math.prod args. And now we can perform this operation there. Oh whoop, sorry, that's a default Python library thing there. And we get six. If we add on a six, seven, eight, we get 27. If this code were any more Pythonic it'd be eating small birds and rodents at the zoo.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。