1
00:00:04,900 --> 00:00:06,760
ok so in the last video we had a lot of

2
00:00:06,760 --> 00:00:09,490
talk about polymorphism lots of slides

3
00:00:09,490 --> 00:00:11,500
let's now see how this all relates to

4
00:00:11,500 --> 00:00:13,990
python code now we're going to use this

5
00:00:13,990 --> 00:00:16,470
code again in the next section so we're

6
00:00:16,470 --> 00:00:19,420
going to create a new python file so

7
00:00:19,420 --> 00:00:22,750
lets create one new python file and

8
00:00:22,750 --> 00:00:24,160
will call this one ducks

9
00:00:24,160 --> 00:00:30,970
ok and i know it's taking awhile a few

10
00:00:30,970 --> 00:00:33,250
videos ago I mentioned that there was a

11
00:00:33,250 --> 00:00:35,920
discussion on ducks so

12
00:00:35,920 --> 00:00:37,590
we're going to start out now with a duck

13
00:00:37,590 --> 00:00:45,450
class....going to

14
00:00:45,450 --> 00:00:51,900
define a few functions....

15
00:00:51,900 --> 00:00:54,450
...

16
00:00:54,450 --> 00:01:00,360
...

17
00:01:00,360 --> 00:01:06,180
....

18
00:01:06,180 --> 00:01:15,490
...

19
00:01:15,490 --> 00:01:21,430
....

20
00:01:21,430 --> 00:01:27,130
....so it's a very

21
00:01:27,130 --> 00:01:29,830
simple class I haven't even created init

22
00:01:29,830 --> 00:01:32,530
method so our ducks do have three methods

23
00:01:32,530 --> 00:01:35,710
walk swim and quack and by the way

24
00:01:35,710 --> 00:01:37,570
because we haven't created a init

25
00:01:37,570 --> 00:01:39,430
method we are getting a warning here the

26
00:01:39,430 --> 00:01:41,380
method walk maybe static will ignore

27
00:01:41,380 --> 00:01:42,220
that for now

28
00:01:42,220 --> 00:01:44,590
alright so what we want to do now is

29
00:01:44,590 --> 00:01:47,890
create a function to test our duck class

30
00:01:47,890 --> 00:01:51,850
so i'm going to come down here and we're

31
00:01:51,850 --> 00:01:57,550
going do...

32
00:01:57,550 --> 00:02:06,050
...

33
00:02:06,050 --> 00:02:09,259
so the test duck function takes a single

34
00:02:09,259 --> 00:02:12,080
argument and calls it's walk swim and

35
00:02:12,080 --> 00:02:14,840
quack methods so i'll create a new duck

36
00:02:14,840 --> 00:02:17,510
and see how it behaves now we may need

37
00:02:17,510 --> 00:02:20,150
to import our ducks into another module so

38
00:02:20,150 --> 00:02:22,520
make sure that the main code doesn't run

39
00:02:22,520 --> 00:02:26,210
if ducks is imported and if you recall we

40
00:02:26,210 --> 00:02:28,130
did see how to do that in a previous

41
00:02:28,130 --> 00:02:31,070
section so let's go ahead....

42
00:02:31,070 --> 00:02:34,310
....

43
00:02:34,310 --> 00:02:37,940
...

44
00:02:37,940 --> 00:02:49,190
...

45
00:02:49,190 --> 00:02:53,960
...so lets run this

46
00:02:53,960 --> 00:02:57,560
you can see down here when I ran it that

47
00:02:57,560 --> 00:03:00,020
donald walks like a duck swims like a

48
00:03:00,020 --> 00:03:02,780
duck and also quacks like a duck

49
00:03:02,780 --> 00:03:04,070
alright so now that we've done that

50
00:03:04,070 --> 00:03:07,310
let's add another class a penguin class

51
00:03:07,310 --> 00:03:10,210
i'm going to add it after the Donald

52
00:03:10,210 --> 00:03:12,350
class finishes leaving two black lines

53
00:03:12,350 --> 00:03:15,460
between it so will call this one

54
00:03:15,460 --> 00:03:22,130
class...

55
00:03:22,130 --> 00:03:29,600
...

56
00:03:29,600 --> 00:03:48,490
...

57
00:03:48,490 --> 00:03:50,860
....

58
00:03:50,860 --> 00:04:01,930
...

59
00:04:01,930 --> 00:04:03,370
...

60
00:04:03,370 --> 00:04:10,420
ok so you can say what we've done there and

61
00:04:10,420 --> 00:04:11,920
added some slang there for those you who not

62
00:04:11,920 --> 00:04:13,270
sure what I've done it are you having a

63
00:04:13,270 --> 00:04:16,360
laugh okay so we've created both the duck

64
00:04:16,360 --> 00:04:20,290
class now and a penguin class now it's clear

65
00:04:20,290 --> 00:04:22,960
that the penguin isn't a duck but it

66
00:04:22,960 --> 00:04:24,990
happens to have exactly the same methods

67
00:04:24,990 --> 00:04:27,760
as our duck class and consequently we

68
00:04:27,760 --> 00:04:30,010
can do exactly the same things with it

69
00:04:30,010 --> 00:04:34,120
and we can do something here like Percy....

70
00:04:34,120 --> 00:04:40,270
....

71
00:04:40,270 --> 00:04:44,950
....let's try running that

72
00:04:44,950 --> 00:04:49,330
so you can see here that our test_

73
00:04:49,330 --> 00:04:51,850
duck function still works now

74
00:04:51,850 --> 00:04:54,070
percy may will be behaving a little bit

75
00:04:54,070 --> 00:04:57,220
strangely by duck standards but as far

76
00:04:57,220 --> 00:04:59,080
as the test_duck functions

77
00:04:59,080 --> 00:05:01,060
concerned percy walks like a duck

78
00:05:01,060 --> 00:05:03,400
swims like a duck and quacks like a duck

79
00:05:03,400 --> 00:05:06,100
as far as the test_duck

80
00:05:06,100 --> 00:05:07,420
function is concerned

81
00:05:07,420 --> 00:05:09,880
Percy is a duck so in other words our

82
00:05:09,880 --> 00:05:13,510
Penguin's passing the duck test what

83
00:05:13,510 --> 00:05:15,310
I'm going to do is switch to the browser now

84
00:05:15,310 --> 00:05:17,230
and open up a link a wikipedia link

85
00:05:17,230 --> 00:05:24,880
paste in the link as always and

86
00:05:24,880 --> 00:05:26,380
close this huge message that's

87
00:05:26,380 --> 00:05:27,850
appearing on the screen there maybe

88
00:05:27,850 --> 00:05:28,420
later

89
00:05:28,420 --> 00:05:33,130
close ok as you can see it says so the

90
00:05:33,130 --> 00:05:35,860
duck test is a humorous term for a

91
00:05:35,860 --> 00:05:38,170
form of reasoning and boils down to

92
00:05:38,170 --> 00:05:40,360
being able to tell what something is by

93
00:05:40,360 --> 00:05:42,790
how it behaves and the phrase is

94
00:05:42,790 --> 00:05:44,830
attributed to an American poet James

95
00:05:44,830 --> 00:05:47,110
Whitcomb raleigh and his original phrase

96
00:05:47,110 --> 00:05:49,270
was when I see a bird that walks like a

97
00:05:49,270 --> 00:05:52,210
duck swims like a duck and quacks like a

98
00:05:52,210 --> 00:05:52,870
duck

99
00:05:52,870 --> 00:05:54,250
I'll call that bird a duck

100
00:05:54,250 --> 00:05:56,890
so what's the point of all this well that

101
00:05:56,890 --> 00:05:59,230
approach is fundamental to the way that

102
00:05:59,230 --> 00:06:02,350
python deals with objects unlike

103
00:06:02,350 --> 00:06:04,870
statically typed languages that require

104
00:06:04,870 --> 00:06:06,700
the type of every object to be declared

105
00:06:06,700 --> 00:06:09,580
when it's used pythons dynamic typing

106
00:06:09,580 --> 00:06:11,590
means that it's only really interested

107
00:06:11,590 --> 00:06:14,530
in what can be done with an object if an

108
00:06:14,530 --> 00:06:16,720
object has the necessary attributes then

109
00:06:16,720 --> 00:06:19,120
python quite happy for it to be used

110
00:06:19,120 --> 00:06:21,640
now the phrase is commonly used in

111
00:06:21,640 --> 00:06:23,710
dynamically type languages like python

112
00:06:23,710 --> 00:06:25,870
and you'll often hear them referred to

113
00:06:25,870 --> 00:06:28,570
as duck type in fact we saw it in one

114
00:06:28,570 --> 00:06:30,160
of the warnings and IntelliJ gave

115
00:06:30,160 --> 00:06:32,290
earlier in the course and that warning

116
00:06:32,290 --> 00:06:34,840
said due to dynamic dispatch and duck typing

117
00:06:34,840 --> 00:06:37,390
this is possible with a limited but

118
00:06:37,390 --> 00:06:39,970
useful number of cases now we know that

119
00:06:39,970 --> 00:06:42,610
penguins aren't ducks and Percy has an

120
00:06:42,610 --> 00:06:45,460
inherited any of these duck like behavior

121
00:06:45,460 --> 00:06:48,310
from a common subclass the important

122
00:06:48,310 --> 00:06:50,020
thing is that percy has the methods

123
00:06:50,020 --> 00:06:52,480
that our test_duck function relies

124
00:06:52,480 --> 00:06:55,450
on and so as far as python is concerned

125
00:06:55,450 --> 00:06:57,850
Percy is a duck so i'll stop the video

126
00:06:57,850 --> 00:06:59,950
here now and in the next video we're

127
00:06:59,950 --> 00:07:00,940
going to have a look at implementing

128
00:07:00,940 --> 00:07:03,400
polymorphism using something called

129
00:07:03,400 --> 00:07:10,620
composition see you in the next video

