1 00:00:00,340 --> 00:00:04,120 In this video, we're going to tackle dunder iter. 2 00:00:04,120 --> 00:00:09,430 This dunder method helps Python know how to handle iterating through an instance, 3 00:00:09,430 --> 00:00:12,490 like you would with while or for loops. 4 00:00:12,490 --> 00:00:15,567 Right now, if we tried to iterate through our instance, 5 00:00:15,567 --> 00:00:17,435 we would get something like this. 6 00:00:38,675 --> 00:00:42,176 Car object is not iterable. 7 00:00:42,176 --> 00:00:47,720 Right now, Python has no clue what you want it to do here, so let's help it out. 8 00:00:48,900 --> 00:00:53,999 Now, it doesn't make a lot of sense to add this method to our car class. 9 00:00:53,999 --> 00:00:59,870 What would make more sense is a group of cars at a car dealership. 10 00:00:59,870 --> 00:01:02,106 Let's create a new class called dealership. 11 00:01:11,380 --> 00:01:12,986 Create our init method. 12 00:01:17,033 --> 00:01:18,943 And give it a list of cars. 13 00:01:38,141 --> 00:01:41,730 Now we have something to iterate through. 14 00:01:41,730 --> 00:01:43,869 Let's add our dunder iter method. 15 00:01:54,242 --> 00:02:00,489 Inside of our method we'll add yield from self.cars. 16 00:02:01,500 --> 00:02:07,420 Yield from is a keyword that tells Python to grab each item in the iterable 17 00:02:07,420 --> 00:02:11,670 we're giving it here the cars list and return it. 18 00:02:13,700 --> 00:02:14,840 Let's see it in action. 19 00:02:15,840 --> 00:02:21,440 First, create an instance and then loop through our cars with the for loop. 20 00:02:47,405 --> 00:02:55,305 Run it, and the console shows our cars. 21 00:02:56,700 --> 00:03:00,850 Now, how can we combine our two classes? 22 00:03:00,850 --> 00:03:05,020 We've got one for cars, and one for a dealership. 23 00:03:05,020 --> 00:03:06,340 Seems like a match to me. 24 00:03:07,480 --> 00:03:09,940 Let's remove our cars in the list here. 25 00:03:09,940 --> 00:03:11,392 So we're back at an empty list. 26 00:03:15,033 --> 00:03:19,520 Then we can add a method that lets us add cars to our dealership. 27 00:03:20,530 --> 00:03:24,236 We'll call this method add_cars. 28 00:03:30,803 --> 00:03:34,220 It'll take self and a car for arguments. 29 00:03:36,500 --> 00:03:39,914 And inside the method we'll append the car to our list. 30 00:03:46,113 --> 00:03:46,613 Perfect! 31 00:03:48,420 --> 00:03:52,770 Now we can use this method to add some cars to our dealership. 32 00:03:52,770 --> 00:03:55,652 Let's create a few car instances and add them 33 00:04:35,151 --> 00:04:39,389 Copy and paste to make it a little bit faster. 34 00:04:45,083 --> 00:04:48,884 Now when we run the file, 35 00:04:48,884 --> 00:04:54,140 we see our new cars in the console. 36 00:04:55,750 --> 00:05:01,620 Now, if you didn't notice we've got two dunder methods working together. 37 00:05:01,620 --> 00:05:05,380 If we comment out the dunder string method on our car class, 38 00:05:13,603 --> 00:05:18,561 And run the file, you can 39 00:05:18,561 --> 00:05:23,110 see we're back at having these object messages in the console. 40 00:05:23,110 --> 00:05:26,607 This is because Python is going into our dealership, 41 00:05:30,701 --> 00:05:32,840 And grabbing our list of cars. 42 00:05:35,502 --> 00:05:42,060 Then when each car instance is printed out, it activates my trap cards. 43 00:05:43,940 --> 00:05:49,025 No, but it does activate the dunder string method of our car class 44 00:05:52,330 --> 00:05:57,350 Nice work, take a few minutes to try this out on your own. 45 00:05:57,350 --> 00:06:01,674 You can create two classes like a book and bookcase, or 46 00:06:01,674 --> 00:06:05,443 candy and candy store to practice dunder iter. 47 00:06:05,443 --> 00:06:07,970 Check the teachers notes to see what I came up with. 48 00:06:09,130 --> 00:06:10,060 Keep up the hard work.