1 00:00:00,150 --> 00:00:08,310 We have discussed that if we pass and I did a bill to the ITER function it returns a nice data object 2 00:00:10,010 --> 00:00:17,450 and on this ITER data object we can call the special method double underscore next. 3 00:00:18,050 --> 00:00:26,480 Similarly when you parse the item data object to the ITER function in this case as well it returns itself 4 00:00:27,020 --> 00:00:37,880 or the either data object itself and on this ITER rated object we can call the method double underscored 5 00:00:38,510 --> 00:00:40,420 next. 6 00:00:40,430 --> 00:00:48,320 So the purpose of the ITER function or the they shouldn't matter double underscore ITC R is to return 7 00:00:48,650 --> 00:00:55,700 an object on which the special method double underscored next can be called. 8 00:00:55,730 --> 00:01:00,370 So let us now parse the I trouble list. 9 00:01:00,380 --> 00:01:04,370 1 2 The ITER function then execute. 10 00:01:04,940 --> 00:01:11,030 So this has returned the ITER function has return and list iterator object. 11 00:01:11,030 --> 00:01:17,600 Now let us pass a string to the ITER function and then execute. 12 00:01:17,600 --> 00:01:20,810 So this is right on a string nitrate that object. 13 00:01:20,810 --> 00:01:27,240 Similarly let's parse a dictionary to the ITER function and then execute. 14 00:01:27,470 --> 00:01:32,180 So this is written a dictionary key ITER later. 15 00:01:32,200 --> 00:01:42,470 Now letters pass a list to the ITER function and this is assigned to the variable ITER one which makes 16 00:01:43,010 --> 00:01:46,850 the variable ITER 1 a list I directed object. 17 00:01:46,910 --> 00:01:49,620 So here we have the list iterator. 18 00:01:49,670 --> 00:01:57,590 Now we have also discussed that when we pass an item that object to the ITER function it returns the 19 00:01:57,710 --> 00:02:00,020 idea that object itself. 20 00:02:00,020 --> 00:02:01,790 So let's see that. 21 00:02:01,790 --> 00:02:06,500 Let's parse the object ITER one execute. 22 00:02:06,500 --> 00:02:11,240 So this has returned the iterator object itself. 23 00:02:11,300 --> 00:02:19,310 The ITER function in order to verify if the ITER function has return a list iterator object we can use 24 00:02:19,340 --> 00:02:21,090 the double equal to operator. 25 00:02:21,890 --> 00:02:23,540 So let's see. 26 00:02:23,540 --> 00:02:30,590 So I have passed the ITER rated object to the ITER function and using the double equal to. 27 00:02:30,590 --> 00:02:39,100 Operator I'm gonna check if this is same as the ITER object IDR 1 or not. 28 00:02:39,140 --> 00:02:46,350 So let's execute and this is true in order for the class to be either rated or word. 29 00:02:47,180 --> 00:02:50,180 We need to define these special methods. 30 00:02:50,810 --> 00:03:01,490 I did and next and we know that the ITER method returns the ITER object itself. 31 00:03:01,820 --> 00:03:09,500 So when you define the ITER method in your eye trouble class you need to have a written statement that 32 00:03:09,620 --> 00:03:18,380 they don't sell returning cell from a method implies that your method returns a reference to the same 33 00:03:18,470 --> 00:03:21,480 instance object on which it was called. 34 00:03:21,740 --> 00:03:31,110 And if you do not want your class to be a nitrate or in that case the IDR method need not add it on 35 00:03:31,250 --> 00:03:35,660 self coming to the special method next. 36 00:03:35,660 --> 00:03:48,270 This contains the logic to compute the next element in the iterator object the method ITC at and next. 37 00:03:48,440 --> 00:03:57,590 Collectively call the ITER data protocol in Python and I directed object implement the ITER data protocol 38 00:03:57,950 --> 00:04:01,470 which consists of these two methods. 39 00:04:01,550 --> 00:04:08,660 So we use the idea data protocol when we have a custom object that we want to be a durable. 40 00:04:08,690 --> 00:04:15,410 We will discuss some examples to understand how custom objects can be iterated over. 41 00:04:16,280 --> 00:04:25,520 So we have an input sequence a which is an eye trouble that is a list now using the ITER and the next 42 00:04:25,520 --> 00:04:26,290 method. 43 00:04:26,360 --> 00:04:30,620 We want to either it or what the elements in this durable. 44 00:04:30,920 --> 00:04:37,100 So instead of using a for loop we are going to manually loop over the elements in the list. 45 00:04:37,100 --> 00:04:40,910 So for that we have defined the class example 1. 46 00:04:41,030 --> 00:04:42,940 So we have defined an init method. 47 00:04:42,950 --> 00:04:50,710 No order to initialize the parameters and then we have defined the IATA and the next method. 48 00:04:50,780 --> 00:04:54,800 The IGAD method has two statements. 49 00:04:54,800 --> 00:05:02,810 We know that the next method needs a nitrate or object in order to iterate over the elements. 50 00:05:02,810 --> 00:05:09,640 So we are going to convert the input sequence which is an either a bill in to an idea later object using 51 00:05:09,640 --> 00:05:17,950 the IDR method and then we need to have a written statement which is going to return the iterated object 52 00:05:18,310 --> 00:05:19,060 itself. 53 00:05:19,060 --> 00:05:28,270 Now coming to the next method within the next method we are going to use a while loop and index numbers 54 00:05:28,600 --> 00:05:35,680 in order to iterate over the elements in the item data object and within the by loop we have used the 55 00:05:35,710 --> 00:05:37,680 try except. 56 00:05:37,960 --> 00:05:39,510 Indeed try blah. 57 00:05:39,580 --> 00:05:47,830 We have this statement and in case this statement raises an error that is if there are no more elements 58 00:05:47,950 --> 00:05:50,610 in the item later object. 59 00:05:51,100 --> 00:05:54,880 In that case we are going to catch this exception. 60 00:05:54,880 --> 00:05:56,900 The exception is index error. 61 00:05:56,950 --> 00:05:58,960 We are going to raise this error. 62 00:05:59,030 --> 00:06:07,990 Stop iteration and if if this statement does not raise any error that guess we are going to print the 63 00:06:08,200 --> 00:06:11,580 item or the element and break out of the loop. 64 00:06:11,800 --> 00:06:14,370 The stop iteration error. 65 00:06:14,410 --> 00:06:15,390 We are raising. 66 00:06:15,400 --> 00:06:22,680 If there are no element in the item that object is used to signal the end of the item nation. 67 00:06:22,870 --> 00:06:30,870 So when there are no more values left to compute the next true method is required to raise a stop iteration. 68 00:06:30,910 --> 00:06:31,630 Exception. 69 00:06:31,630 --> 00:06:37,890 So when we see these top iteration error we know that we have exhausted the item data. 70 00:06:38,170 --> 00:06:42,330 So this is normal behavior in all ITER does. 71 00:06:42,370 --> 00:06:44,360 So we have defined the class. 72 00:06:44,410 --> 00:06:48,820 Now let us go and create an instance. 73 00:06:48,820 --> 00:06:58,590 Let us call this TRL 1 and we are going to pass the list a to the class. 74 00:06:58,780 --> 00:06:59,630 Example 1. 75 00:06:59,680 --> 00:07:00,950 Then execute. 76 00:07:01,540 --> 00:07:05,080 So here is the instance object. 77 00:07:05,080 --> 00:07:11,700 No let us convert the instant object to an item later object by passing it to the ITER function. 78 00:07:11,820 --> 00:07:13,480 So let's execute this. 79 00:07:13,480 --> 00:07:17,600 So IDR 1 is an item later object. 80 00:07:17,640 --> 00:07:23,380 We now have an operator object which is generated by the IDR function. 81 00:07:23,380 --> 00:07:28,630 So on this I directed object we can call the next function. 82 00:07:28,690 --> 00:07:33,190 So let us parse this object to the next function. 83 00:07:33,340 --> 00:07:41,180 So it has generated the next function has generated the first item in the item that object. 84 00:07:41,320 --> 00:07:42,830 I d e r 1. 85 00:07:42,910 --> 00:07:50,780 Now if we want to compute or generate the next item in the sequence we need to call the next method 86 00:07:50,900 --> 00:07:51,780 again. 87 00:07:52,030 --> 00:07:57,270 So let's call the next method again on ITV 1. 88 00:07:57,340 --> 00:08:03,290 And here is the next Matar and here is the next round you select. 89 00:08:03,310 --> 00:08:09,880 Keep calling the next function on the idea that object 4 5. 90 00:08:09,970 --> 00:08:18,640 So we have defined five elements in the list and then the next function or the next method has reached 91 00:08:18,640 --> 00:08:21,550 the end of the item later object. 92 00:08:21,550 --> 00:08:30,140 The next function has encountered index error and this stop operation exception has been DREs. 93 00:08:30,160 --> 00:08:38,560 If you call the next function again and again on the same I directed object it is going to raise the 94 00:08:38,560 --> 00:08:41,500 same exception stop ideation. 95 00:08:41,500 --> 00:08:47,890 This is because there are no values or no more values to be computed in this outdated object. 96 00:08:48,670 --> 00:08:58,350 So there is no use of the item data object when all of these values have been consumed in this object. 97 00:08:58,390 --> 00:09:06,640 So if you want to generate the same set of numbers you can recreate the same object or you can create 98 00:09:06,760 --> 00:09:10,890 a new instance object with the same input sequence. 99 00:09:10,900 --> 00:09:17,810 Now let us try to generate an arithmetic sequence using a nitrate object. 100 00:09:18,130 --> 00:09:24,640 So an added Matic sequence is a sequence of numbers such that the difference between the consecutive 101 00:09:24,640 --> 00:09:27,190 terms is constant. 102 00:09:27,190 --> 00:09:32,590 Difference here means the second term minus the first term. 103 00:09:32,620 --> 00:09:40,570 So the difference between the second and the first terms is three difference between the third and the 104 00:09:40,570 --> 00:09:42,840 second term is three. 105 00:09:42,880 --> 00:09:46,180 The third and the second terms is three again and so on. 106 00:09:46,270 --> 00:09:55,690 And this is the formula to calculate the end term in the sequence in here represents the number of the 107 00:09:55,690 --> 00:09:58,130 tone in the particular sequence. 108 00:09:58,150 --> 00:10:03,790 So let us use the same formula to generate this arithmetic sequence. 109 00:10:04,060 --> 00:10:11,860 So this is the class arithmetic progression which has implemented the I treated protocol which means 110 00:10:12,160 --> 00:10:22,140 the special method I.D. card and next method if you'll notice the 80 odd method is going to return itself 111 00:10:22,230 --> 00:10:26,320 which means it is going to return the iterator object itself. 112 00:10:26,530 --> 00:10:32,130 So let us go ahead and create an instance of this class arithmetic progression. 113 00:10:32,300 --> 00:10:40,950 Let's call this instance a b 1 and let's parse the arguments the first term is 1. 114 00:10:41,260 --> 00:10:51,040 We want to generate the first 10 items in the sequence and then the difference between the consecutive 115 00:10:51,340 --> 00:10:54,360 numbers in this sequence should be three. 116 00:10:54,370 --> 00:10:56,710 So let instantiate the object. 117 00:10:56,710 --> 00:11:03,400 So here we have the object AP 1 let's pass it to the TR function. 118 00:11:03,460 --> 00:11:12,460 Now on this item data object we are going to call the next function the next function has generated 119 00:11:12,460 --> 00:11:15,160 the first term in this sequence. 120 00:11:15,160 --> 00:11:20,240 So let's keep calling the next function on this. 121 00:11:20,410 --> 00:11:30,850 I directed object or is the next number in the sequence 7 is the next number in the sequence 10 13 16 122 00:11:31,130 --> 00:11:33,370 22 twenty eight. 123 00:11:33,700 --> 00:11:40,930 And then after the fourth 10 numbers in the sequence have been generated this top iteration exception 124 00:11:41,140 --> 00:11:44,800 has been raised by the next function. 125 00:11:44,800 --> 00:11:52,180 Even if you call the next function again or wouldn't work the item rate or object it's going to raise 126 00:11:52,180 --> 00:11:56,920 the same exception because there are no more values to be computed.