Packing, a Practical Example3:38 with Ashley Boucher
See packing in action with a practical example.
In this video, 0:00 we're going to demonstrate the power of *args with a practical example. 0:01 I've made a new file here in the workspace called cart.py. 0:05 For this example, let's say we're building a simple shopping cart app. 0:08 We'll write a function called calculate_total. 0:12 The job of calculate_total is to add up the price of every item in 0:20 the shopping cart. 0:23 If I was coding this with positional arguments, 0:25 I might add parameters like item1, item2, item three. 0:28 But where do I stop? 0:33 How do I know how many parameters to add? 0:35 The short answer is I don't. 0:37 And neither does our function. 0:38 It can't know ahead of time how many items someone will add to their shopping cart. 0:40 Using positional arguments for every item won't work. 0:45 This is where *args comes in. 0:48 By using *args as our parameter, we can pass as many 0:50 items to the function as we want and the function will always work the same. 0:53 So let's edit what I just did. 0:57 Now that I've changed the parameter to *args, 1:03 I can write the body of the function. 1:05 On the right side of this assignment here, I'm passing args, in other words a tuple 1:14 containing the price of every item in the shopping cart to the sum method. 1:18 I have no idea how many items this tuple will contain when calculate_total function 1:23 is called, but I don't need to because it works the same whether it's 1 item or 1:27 20 or 50. 1:31 The sum method by the way receives a sequence usually comprised of integers. 1:32 It adds up the value of every item in that sequence and then returns the sum. 1:38 In this example, we're assigning that sum to the total variable, 1:42 then we can print it, return it or do something else entirely with that value. 1:45 For now, I'll choose to print it so we can easily see what's going on. 1:50 Calling the calculate_total function looks just like any other function calls. 1:54 So let's see an example. 2:01 I'll pass four values to this function representing the prices of four imaginary 2:08 items in my shopping cart. 2:13 Let's say it's 25, 25, 20, and 30. 2:15 Now, I'll save and run this, and we'll take a look. 2:18 I can expect that the sum of all of these prices is gonna be $100, let's see. 2:22 Yep, all we see here is that it printed 100, 2:33 the sum of the four prices I passed to the function. 2:35 But look, if I call calculate_total again and 2:38 send five prices instead of four, the function will work just as well. 2:41 Now the printer value is 200, as expected. 2:49 And if I only wanna send one price, 2:52 We see 25 is printed. 3:00 This is the beauty of *args. 3:02 You can build a function that is ready to handle whatever data is thrown at it. 3:04 Awesome. 3:08 Now it is time for you to try this on your own. 3:09 Before moving on to the next video, give this exercise a try. 3:11 Your instructions are to write a short function called Packer. 3:15 Any argument sent to Packer should be packed up into a *args parameter. 3:18 In the body of the function, do something with args like print it out or 3:23 loop through it. 3:26 Play around with calling the function as well. 3:27 Pass different numbers of arguments to Packer and see what the outcome is. 3:29 This is an opportunity to try things and see cause and effect. 3:33 Have fun. 3:37
You need to sign up for Treehouse in order to download course files.Sign up