WEBVTT 1 00:00:01.189 --> 00:00:03.500 ok so in the last video we had a lot of 2 00:00:03.500 --> 00:00:06.230 talk about polymorphism lots of slides ok so in the last video we had a lot of 3 00:00:06.230 --> 00:00:08.240 let's now see how this all relates to talk about polymorphism lots of slides 4 00:00:08.240 --> 00:00:10.730 python code now we're going to use this let's now see how this all relates to 5 00:00:10.730 --> 00:00:13.219 code again in the next section so we're python code now we're going to use this 6 00:00:13.219 --> 00:00:16.160 going to create a new python file so code again in the next section so we're 7 00:00:16.160 --> 00:00:19.490 lets create one new python file and going to create a new python file so 8 00:00:19.490 --> 00:00:20.900 will call this one ducks lets create one new python file and 9 00:00:20.900 --> 00:00:27.710 ok and i know it's taking awhile a few will call this one ducks 10 00:00:27.710 --> 00:00:29.990 videos ago I mentioned that there was a ok and i know it's taking awhile a few 11 00:00:29.990 --> 00:00:32.660 discussion on ducks so videos ago I mentioned that there was a 12 00:00:32.660 --> 00:00:34.339 we're going to start out now with a duck discussion on ducks so 13 00:00:34.339 --> 00:00:42.199 class....going to we're going to start out now with a duck 14 00:00:42.199 --> 00:00:48.649 define a few functions.... class....going to 15 00:00:48.649 --> 00:00:51.199 ... define a few functions.... 16 00:00:51.199 --> 00:00:57.109 ... ... 17 00:00:57.109 --> 00:01:02.929 .... ... 18 00:01:02.929 --> 00:01:12.230 ... .... 19 00:01:12.230 --> 00:01:18.170 .... ... 20 00:01:18.170 --> 00:01:23.870 ....so it's a very .... 21 00:01:23.870 --> 00:01:26.570 simple class I haven't even created init ....so it's a very 22 00:01:26.570 --> 00:01:29.270 method so our ducks do have three methods simple class I haven't even created init 23 00:01:29.270 --> 00:01:32.450 walk swim and quack and by the way method so our ducks do have three methods 24 00:01:32.450 --> 00:01:34.310 because we haven't created a init walk swim and quack and by the way 25 00:01:34.310 --> 00:01:36.170 method we are getting a warning here the because we haven't created a init 26 00:01:36.170 --> 00:01:38.120 method walk maybe static will ignore method we are getting a warning here the 27 00:01:38.120 --> 00:01:38.960 that for now method walk maybe static will ignore 28 00:01:38.960 --> 00:01:41.330 alright so what we want to do now is that for now 29 00:01:41.330 --> 00:01:44.630 create a function to test our duck class alright so what we want to do now is 30 00:01:44.630 --> 00:01:48.590 so i'm going to come down here and we're create a function to test our duck class 31 00:01:48.590 --> 00:01:54.290 going do... so i'm going to come down here and we're 32 00:01:54.290 --> 00:02:02.790 ... going do... 33 00:02:02.790 --> 00:02:06.000 so the test duck function takes a single ... 34 00:02:06.000 --> 00:02:08.820 argument and calls it's walk swim and so the test duck function takes a single 35 00:02:08.820 --> 00:02:11.580 quack methods so i'll create a new duck argument and calls it's walk swim and 36 00:02:11.580 --> 00:02:14.250 and see how it behaves now we may need quack methods so i'll create a new duck 37 00:02:14.250 --> 00:02:16.890 to import our ducks into another module so and see how it behaves now we may need 38 00:02:16.890 --> 00:02:19.260 make sure that the main code doesn't run to import our ducks into another module so 39 00:02:19.260 --> 00:02:22.950 if ducks is imported and if you recall we make sure that the main code doesn't run 40 00:02:22.950 --> 00:02:24.870 did see how to do that in a previous if ducks is imported and if you recall we 41 00:02:24.870 --> 00:02:27.810 section so let's go ahead.... did see how to do that in a previous 42 00:02:27.810 --> 00:02:31.050 .... section so let's go ahead.... 43 00:02:31.050 --> 00:02:34.680 ... .... 44 00:02:34.680 --> 00:02:45.930 ... ... 45 00:02:45.930 --> 00:02:50.700 ...so lets run this ... 46 00:02:50.700 --> 00:02:54.300 you can see down here when I ran it that ...so lets run this 47 00:02:54.300 --> 00:02:56.760 donald walks like a duck swims like a you can see down here when I ran it that 48 00:02:56.760 --> 00:02:59.520 duck and also quacks like a duck donald walks like a duck swims like a 49 00:02:59.520 --> 00:03:00.810 alright so now that we've done that duck and also quacks like a duck 50 00:03:00.810 --> 00:03:04.050 let's add another class a penguin class alright so now that we've done that 51 00:03:04.050 --> 00:03:06.959 i'm going to add it after the Donald let's add another class a penguin class 52 00:03:06.959 --> 00:03:09.090 class finishes leaving two black lines i'm going to add it after the Donald 53 00:03:09.090 --> 00:03:12.209 between it so will call this one class finishes leaving two black lines 54 00:03:12.209 --> 00:03:18.870 class... between it so will call this one 55 00:03:18.870 --> 00:03:26.340 ... class... 56 00:03:26.340 --> 00:03:45.230 ... ... 57 00:03:45.230 --> 00:03:47.600 .... ... 58 00:03:47.600 --> 00:03:58.670 ... .... 59 00:03:58.670 --> 00:04:00.110 ... ... 60 00:04:00.110 --> 00:04:07.160 ok so you can say what we've done there and ... 61 00:04:07.160 --> 00:04:08.660 added some slang there for those you who not ok so you can say what we've done there and 62 00:04:08.660 --> 00:04:10.010 sure what I've done it are you having a added some slang there for those you who not 63 00:04:10.010 --> 00:04:13.100 laugh okay so we've created both the duck sure what I've done it are you having a 64 00:04:13.100 --> 00:04:17.030 class now and a penguin class now it's clear laugh okay so we've created both the duck 65 00:04:17.030 --> 00:04:19.700 that the penguin isn't a duck but it class now and a penguin class now it's clear 66 00:04:19.700 --> 00:04:21.739 happens to have exactly the same methods that the penguin isn't a duck but it 67 00:04:21.739 --> 00:04:24.500 as our duck class and consequently we happens to have exactly the same methods 68 00:04:24.500 --> 00:04:26.750 can do exactly the same things with it as our duck class and consequently we 69 00:04:26.750 --> 00:04:30.860 and we can do something here like Percy.... can do exactly the same things with it 70 00:04:30.860 --> 00:04:37.010 .... and we can do something here like Percy.... 71 00:04:37.010 --> 00:04:41.690 ....let's try running that .... 72 00:04:41.690 --> 00:04:46.070 so you can see here that our test_ ....let's try running that 73 00:04:46.070 --> 00:04:48.590 duck function still works now so you can see here that our test_ 74 00:04:48.590 --> 00:04:50.810 percy may will be behaving a little bit duck function still works now 75 00:04:50.810 --> 00:04:53.960 strangely by duck standards but as far percy may will be behaving a little bit 76 00:04:53.960 --> 00:04:55.820 as the test_duck functions strangely by duck standards but as far 77 00:04:55.820 --> 00:04:57.800 concerned percy walks like a duck as the test_duck functions 78 00:04:57.800 --> 00:05:00.140 swims like a duck and quacks like a duck concerned percy walks like a duck 79 00:05:00.140 --> 00:05:02.840 as far as the test_duck swims like a duck and quacks like a duck 80 00:05:02.840 --> 00:05:04.160 function is concerned as far as the test_duck 81 00:05:04.160 --> 00:05:06.620 Percy is a duck so in other words our function is concerned 82 00:05:06.620 --> 00:05:10.250 Penguin's passing the duck test what Percy is a duck so in other words our 83 00:05:10.250 --> 00:05:12.050 I'm going to do is switch to the browser now Penguin's passing the duck test what 84 00:05:12.050 --> 00:05:13.970 and open up a link a wikipedia link I'm going to do is switch to the browser now 85 00:05:13.970 --> 00:05:21.620 paste in the link as always and and open up a link a wikipedia link 86 00:05:21.620 --> 00:05:23.120 close this huge message that's paste in the link as always and 87 00:05:23.120 --> 00:05:24.590 appearing on the screen there maybe close this huge message that's 88 00:05:24.590 --> 00:05:25.160 later appearing on the screen there maybe 89 00:05:25.160 --> 00:05:29.870 close ok as you can see it says so the later 90 00:05:29.870 --> 00:05:32.600 duck test is a humorous term for a close ok as you can see it says so the 91 00:05:32.600 --> 00:05:34.910 form of reasoning and boils down to duck test is a humorous term for a 92 00:05:34.910 --> 00:05:37.100 being able to tell what something is by form of reasoning and boils down to 93 00:05:37.100 --> 00:05:39.530 how it behaves and the phrase is being able to tell what something is by 94 00:05:39.530 --> 00:05:41.570 attributed to an American poet James how it behaves and the phrase is 95 00:05:41.570 --> 00:05:43.850 Whitcomb raleigh and his original phrase attributed to an American poet James 96 00:05:43.850 --> 00:05:46.010 was when I see a bird that walks like a Whitcomb raleigh and his original phrase 97 00:05:46.010 --> 00:05:48.950 duck swims like a duck and quacks like a was when I see a bird that walks like a 98 00:05:48.950 --> 00:05:49.610 duck duck swims like a duck and quacks like a 99 00:05:49.610 --> 00:05:50.990 I'll call that bird a duck duck 100 00:05:50.990 --> 00:05:53.630 so what's the point of all this well that I'll call that bird a duck 101 00:05:53.630 --> 00:05:55.970 approach is fundamental to the way that so what's the point of all this well that 102 00:05:55.970 --> 00:05:59.090 python deals with objects unlike approach is fundamental to the way that 103 00:05:59.090 --> 00:06:01.610 statically typed languages that require python deals with objects unlike 104 00:06:01.610 --> 00:06:03.440 the type of every object to be declared statically typed languages that require 105 00:06:03.440 --> 00:06:06.320 when it's used pythons dynamic typing the type of every object to be declared 106 00:06:06.320 --> 00:06:08.330 means that it's only really interested when it's used pythons dynamic typing 107 00:06:08.330 --> 00:06:11.270 in what can be done with an object if an means that it's only really interested 108 00:06:11.270 --> 00:06:13.460 object has the necessary attributes then in what can be done with an object if an 109 00:06:13.460 --> 00:06:15.860 python quite happy for it to be used object has the necessary attributes then 110 00:06:15.860 --> 00:06:18.380 now the phrase is commonly used in python quite happy for it to be used 111 00:06:18.380 --> 00:06:20.450 dynamically type languages like python now the phrase is commonly used in 112 00:06:20.450 --> 00:06:22.610 and you'll often hear them referred to dynamically type languages like python 113 00:06:22.610 --> 00:06:25.310 as duck type in fact we saw it in one and you'll often hear them referred to 114 00:06:25.310 --> 00:06:26.900 of the warnings and IntelliJ gave as duck type in fact we saw it in one 115 00:06:26.900 --> 00:06:29.030 earlier in the course and that warning of the warnings and IntelliJ gave 116 00:06:29.030 --> 00:06:31.580 said due to dynamic dispatch and duck typing earlier in the course and that warning 117 00:06:31.580 --> 00:06:34.130 this is possible with a limited but said due to dynamic dispatch and duck typing 118 00:06:34.130 --> 00:06:36.710 useful number of cases now we know that this is possible with a limited but 119 00:06:36.710 --> 00:06:39.350 penguins aren't ducks and Percy has an useful number of cases now we know that 120 00:06:39.350 --> 00:06:42.200 inherited any of these duck like behavior penguins aren't ducks and Percy has an 121 00:06:42.200 --> 00:06:45.050 from a common subclass the important inherited any of these duck like behavior 122 00:06:45.050 --> 00:06:46.760 thing is that percy has the methods from a common subclass the important 123 00:06:46.760 --> 00:06:49.220 that our test_duck function relies thing is that percy has the methods 124 00:06:49.220 --> 00:06:52.190 on and so as far as python is concerned that our test_duck function relies 125 00:06:52.190 --> 00:06:54.590 Percy is a duck so i'll stop the video on and so as far as python is concerned 126 00:06:54.590 --> 00:06:56.690 here now and in the next video we're Percy is a duck so i'll stop the video 127 00:06:56.690 --> 00:06:57.680 going to have a look at implementing here now and in the next video we're 128 00:06:57.680 --> 00:07:00.140 polymorphism using something called going to have a look at implementing 129 00:07:00.140 --> 00:07:07.360 composition see you in the next video polymorphism using something called