1
1

00:00:00,650  -->  00:00:02,120
<v ->Hi guys and welcome back.</v>
2

2

00:00:02,120  -->  00:00:05,710
In this video we're going to learn about class composition.
3

3

00:00:05,710  -->  00:00:08,250
Composition is a counterpart to inheritance
4

4

00:00:08,250  -->  00:00:11,530
so you build out classes that use other classes.
5

5

00:00:11,530  -->  00:00:14,660
You're going to be using inheritance very little in Python,
6

6

00:00:14,660  -->  00:00:15,493
if I'm honest,
7

7

00:00:15,493  -->  00:00:18,240
you're going to be using composition much more.
8

8

00:00:18,240  -->  00:00:21,000
Composition allows your classes to be simpler
9

9

00:00:21,000  -->  00:00:23,550
and reduces the complexity of your code overall,
10

10

00:00:23,550  -->  00:00:27,100
so it is recommended that you use that most of the time.
11

11

00:00:27,100  -->  00:00:29,820
In addition, composition means something different
12

12

00:00:29,820  -->  00:00:32,580
than inheritance when you're thinking about your classes.
13

13

00:00:32,580  -->  00:00:35,800
For example, we've got here a bookshelf class
14

14

00:00:35,800  -->  00:00:38,140
that takes in a quantity when you create it
15

15

00:00:38,140  -->  00:00:39,800
and then when you print it out it tells you
16

16

00:00:39,800  -->  00:00:41,990
that it's a bookshelf with x books.
17

17

00:00:41,990  -->  00:00:44,540
For example, here we're creating a bookshelf with 300 books,
18

18

00:00:44,540  -->  00:00:45,510
when you print it out,
19

19

00:00:45,510  -->  00:00:48,450
you can see that it says bookshelf with 300 books.
20

20

00:00:48,450  -->  00:00:51,570
If you wanted to create a book class now,
21

21

00:00:51,570  -->  00:00:53,630
some new programme has maybe attempted
22

22

00:00:53,630  -->  00:00:55,440
to make it inherit from bookshelf.
23

23

00:00:55,440  -->  00:00:57,440
After all, they are somehow related.
24

24

00:00:57,440  -->  00:00:58,273
Correct?
25

25

00:00:58,273  -->  00:00:59,533
Books live in bookshelves so.
26

26

00:01:00,520  -->  00:01:02,880
And then you would do something like this in it
27

27

00:01:02,880  -->  00:01:06,040
and then you would say the name of the book.
28

28

00:01:06,040  -->  00:01:10,860
But now if you want this book to call the superclasses
29

29

00:01:10,860  -->  00:01:14,520
in each method, you also need the quantity of books.
30

30

00:01:14,520  -->  00:01:17,100
So this is where this is trying to break down a little bit,
31

31

00:01:17,100  -->  00:01:21,630
a book object doesn't have a quantity of anything.
32

32

00:01:21,630  -->  00:01:23,810
So anyway, we are going to continue with that example
33

33

00:01:23,810  -->  00:01:26,080
because we firmly believe that
34

34

00:01:26,080  -->  00:01:28,270
a book should inherit from bookshelf.
35

35

00:01:28,270  -->  00:01:30,020
So you'll put your quantity in here
36

36

00:01:31,270  -->  00:01:33,180
and then you'll say self.name equal name.
37

37

00:01:33,180  -->  00:01:34,630
Okay, perfect.
38

38

00:01:34,630  -->  00:01:38,320
Now, this I'm sure you'll realise makes little sense
39

39

00:01:38,320  -->  00:01:42,600
because you are passing the quantity to a book.
40

40

00:01:42,600  -->  00:01:44,700
So when you create a book object,
41

41

00:01:44,700  -->  00:01:47,100
you're going to have to say Harry Potter
42

42

00:01:47,100  -->  00:01:50,780
and then you're going to have to say something like 120.
43

43

00:01:50,780  -->  00:01:52,830
Then if you print book,
44

44

00:01:52,830  -->  00:01:55,890
you'll get something out that's not all that good.
45

45

00:01:55,890  -->  00:02:00,630
You get bookshelf with 120 books when you print the book.
46

46

00:02:00,630  -->  00:02:05,630
So clearly you're going to need an str method here
47

47

00:02:05,820  -->  00:02:07,390
that prints something completely different.
48

48

00:02:07,390  -->  00:02:11,880
So for example, book self.name.
49

49

00:02:11,880  -->  00:02:13,720
Okay, let's press play again
50

50

00:02:13,720  -->  00:02:15,370
and now you get book Harry Potter.
51

51

00:02:15,370  -->  00:02:19,610
So why is this a bad approach?
52

52

00:02:19,610  -->  00:02:21,080
Well, there are two reasons.
53

53

00:02:21,080  -->  00:02:24,500
One of them is conceptual, the way of thinking about it.
54

54

00:02:24,500  -->  00:02:26,470
The other one is technical.
55

55

00:02:26,470  -->  00:02:30,610
The conceptual reason is that when you do inheritance,
56

56

00:02:30,610  -->  00:02:32,130
you are essentially treating it
57

57

00:02:32,130  -->  00:02:34,270
like evolutionary inheritance.
58

58

00:02:34,270  -->  00:02:39,270
You're saying that a book is a bookshelf and something more.
59

59

00:02:41,170  -->  00:02:45,880
For example, in the same way that all tigers are mammals,
60

60

00:02:45,880  -->  00:02:48,010
but not all mammals are tigers.
61

61

00:02:48,010  -->  00:02:50,180
So all books are bookshelves,
62

62

00:02:50,180  -->  00:02:52,120
but not all bookshelves are books.
63

63

00:02:52,120  -->  00:02:54,970
The bookshelf can still be used on its own.
64

64

00:02:54,970  -->  00:02:58,160
That breaks down when you talk about books and bookshelves,
65

65

00:02:58,160  -->  00:03:00,340
because a book is something completely different
66

66

00:03:00,340  -->  00:03:01,220
from a bookshelf.
67

67

00:03:01,220  -->  00:03:06,160
A bookshelf can contain books, but one isn't the other.
68

68

00:03:06,160  -->  00:03:08,850
The technical reason why this breaks down
69

69

00:03:08,850  -->  00:03:12,500
is because you've got this book class that inherits
70

70

00:03:12,500  -->  00:03:17,500
from bookshelf, but actually you are not using inside it
71

71

00:03:18,760  -->  00:03:20,230
anything about the bookshelf.
72

72

00:03:20,230  -->  00:03:22,830
So you are completely overriding the str method
73

73

00:03:22,830  -->  00:03:24,810
because you don't want anything to do with bookshelves
74

74

00:03:24,810  -->  00:03:27,900
in there and you actually don't need these at all
75

75

00:03:27,900  -->  00:03:31,930
because I mean, what's the point of setting the quantity
76

76

00:03:31,930  -->  00:03:34,620
if you're not going to use it in the methods?
77

77

00:03:34,620  -->  00:03:38,700
So the conceptual reason is a book is not a bookshelf.
78

78

00:03:38,700  -->  00:03:40,788
The technical reason is there is no reason
79

79

00:03:40,788  -->  00:03:42,880
to inherit if you're not gonna use
80

80

00:03:42,880  -->  00:03:44,460
that inheritance anywhere.
81

81

00:03:44,460  -->  00:03:48,060
So this is where composition comes in.
82

82

00:03:48,060  -->  00:03:52,740
Composition is for when you wanna say something like,
83

83

00:03:52,740  -->  00:03:55,120
a bookshelf has many books.
84

84

00:03:55,120  -->  00:04:00,120
A bookshelf is composed of a bunch of things and books.
85

85

00:04:00,710  -->  00:04:05,580
So instead of defining our bookshelf and our book like this,
86

86

00:04:05,580  -->  00:04:08,150
we're going to define it slightly differently.
87

87

00:04:08,150  -->  00:04:11,550
Instead of setting the quantity of books in the bookshelf,
88

88

00:04:11,550  -->  00:04:14,600
we're actually going to allow the constructor
89

89

00:04:14,600  -->  00:04:16,603
to take in a number of books.
90

90

00:04:17,800  -->  00:04:19,430
So we will do star books
91

91

00:04:19,430  -->  00:04:22,113
and then we'll say self.books, equal books.
92

92

00:04:22,990  -->  00:04:27,990
And then here we will say bookshelf with len of self.books.
93

93

00:04:28,480  -->  00:04:33,480
So what this will take in is a bunch of book objects.
94

94

00:04:35,400  -->  00:04:38,970
Next up, the book class doesn't actually need,
95

95

00:04:38,970  -->  00:04:40,660
I'm going to delete this as well for now,
96

96

00:04:40,660  -->  00:04:43,380
the book class doesn't need to inherit from bookshelf
97

97

00:04:43,380  -->  00:04:45,240
and it doesn't need the quantity
98

98

00:04:45,240  -->  00:04:48,270
and it doesn't need to call the superclass of anything.
99

99

00:04:48,270  -->  00:04:49,200
Okay.
100

100

00:04:49,200  -->  00:04:52,440
So now we have two much simpler classes,
101

101

00:04:52,440  -->  00:04:54,270
but how do you use them?
102

102

00:04:54,270  -->  00:04:56,320
Well, we can create our book
103

103

00:04:56,320  -->  00:04:58,120
and then we can create another book.
104

104

00:05:01,780  -->  00:05:02,613
Just like that.
105

105

00:05:02,613  -->  00:05:03,780
So we have our two books here.
106

106

00:05:03,780  -->  00:05:08,780
Then we'll create our bookshelf and give it the two books.
107

107

00:05:11,810  -->  00:05:13,963
Then we can print the shelf.
108

108

00:05:15,110  -->  00:05:17,180
So I'll press play now and you can see
109

109

00:05:17,180  -->  00:05:20,193
that you get bookshelf with two books printed out.
110

110

00:05:21,280  -->  00:05:24,480
This is composition and this is very common,
111

111

00:05:24,480  -->  00:05:26,250
much more common than inheritance.
112

112

00:05:26,250  -->  00:05:30,170
It is when you have a class that contains
113

113

00:05:30,170  -->  00:05:32,220
a bunch of other classes or a class
114

114

00:05:32,220  -->  00:05:34,640
that has as many of that.
115

115

00:05:34,640  -->  00:05:37,290
And the here we've got our bookshelf that has many books.
116

116

00:05:37,290  -->  00:05:39,330
And when you use it, when you create it,
117

117

00:05:39,330  -->  00:05:43,013
you can pass to it a bunch of your other book objects.
118

118

00:05:44,320  -->  00:05:45,960
So to recap.
119

119

00:05:45,960  -->  00:05:50,080
Inheritance means that a book is a bookshelf,
120

120

00:05:50,080  -->  00:05:53,830
composition means that a bookshelf has many books.
121

121

00:05:53,830  -->  00:05:55,670
So take your pick from those two,
122

122

00:05:55,670  -->  00:05:58,960
that's an important decision to make, but as I said earlier,
123

123

00:05:58,960  -->  00:06:02,190
you'll most frequently be using composition.
124

124

00:06:02,190  -->  00:06:03,540
Thanks for joining me in this video
125

125

00:06:03,540  -->  00:06:05,930
to learn about composition in Python,
126

126

00:06:05,930  -->  00:06:07,580
and I'll see you in the next one.
