1
00:00:05,310 --> 00:00:09,360
Welcome back everyone in this lecture we're going to finish our discussion of object oriented programming

2
00:00:09,630 --> 00:00:11,450
by talking about special methods.

3
00:00:11,460 --> 00:00:15,400
These are also called Dunder methods because of their use of double underscores.

4
00:00:15,600 --> 00:00:18,520
Let's head over to the text editor and finish off our discussion.

5
00:00:18,750 --> 00:00:19,400
All right.

6
00:00:19,470 --> 00:00:22,790
As we already know there's built in functions in Python.

7
00:00:22,860 --> 00:00:28,320
For example there's the built in length function that can check the length of a list or there's the

8
00:00:28,320 --> 00:00:31,530
built in print function that can print out a list.

9
00:00:31,860 --> 00:00:38,730
But notice that Lenk can check the length of a list but it could also check the length of a string.

10
00:00:38,730 --> 00:00:39,660
So how does it do this.

11
00:00:39,660 --> 00:00:44,730
How does length as a built in method know what to return when you ask the length of the list or the

12
00:00:44,730 --> 00:00:47,480
length of the string or the length of any other object.

13
00:00:47,610 --> 00:00:53,250
And what if we want our own objects to be able to interact with built in functions such as Eliane and

14
00:00:53,250 --> 00:00:54,230
Prince.

15
00:00:54,330 --> 00:00:57,090
In that case we can use what's known as special methods.

16
00:00:57,240 --> 00:01:01,980
So we're going to be creating special methods within our own classes or objects in order to be able

17
00:01:01,980 --> 00:01:07,220
to interact with these built in length and print functionalities.

18
00:01:07,300 --> 00:01:15,170
I'm going to create a class and we're going to call this class book and we're going to have an in it

19
00:01:15,170 --> 00:01:25,980
method and our book is going to take in a tidal an author and the number of pages and then we'll say

20
00:01:26,040 --> 00:01:36,450
self-taught title is equal to title self-taught author is equal to author and then self-taught pages

21
00:01:37,020 --> 00:01:40,180
is equal to the pages.

22
00:01:40,220 --> 00:01:46,190
OK so there's our book save this and then let's see what happens if I create an instance of the book

23
00:01:46,330 --> 00:01:52,870
so I'll say my book is equal to an instance of the book and I need to pass in the attributes title author

24
00:01:52,880 --> 00:01:53,810
pages.

25
00:01:54,050 --> 00:01:57,000
So let's say the title is Jose.

26
00:01:57,060 --> 00:02:03,570
Actually let's say the title is python rocks the author is Jose.

27
00:02:03,700 --> 00:02:08,760
And then we'll say number of pages is there's 250 pages in this book so I'm going to save that.

28
00:02:09,010 --> 00:02:10,540
So I've already created my book.

29
00:02:10,540 --> 00:02:15,950
And then what I'm going to do is I'm going to print out my book.

30
00:02:16,030 --> 00:02:18,970
Now what do you think is actually going to print out when I print my book.

31
00:02:18,970 --> 00:02:19,650
Let's find out.

32
00:02:20,560 --> 00:02:25,160
We'll say pass on my example that pi and Currently when you print out my book right now I'm just going

33
00:02:25,160 --> 00:02:29,640
to say hey you have a book object at this location in memory.

34
00:02:29,870 --> 00:02:35,060
Now you can actually decide what gets displayed when you print out my book and the way you can do that

35
00:02:35,110 --> 00:02:39,290
is through the use of a special method and the special method for that.

36
00:02:39,290 --> 00:02:41,000
There's actually two options.

37
00:02:41,030 --> 00:02:46,340
One is underscore underscore SDR underscore underscore that the one we're going to be using since it's

38
00:02:46,340 --> 00:02:54,120
used more often in the flash documentation is our R and that's basically going to ask for what's the

39
00:02:54,120 --> 00:02:58,800
representation for this object when you want it to be printed out.

40
00:02:58,960 --> 00:03:01,790
So I'll say self.

41
00:03:02,030 --> 00:03:08,150
And then we can return whatever we want to be printed out when you print out a book object.

42
00:03:08,150 --> 00:03:12,320
And notice here I'm saying return not print.

43
00:03:12,460 --> 00:03:16,440
So we're going to say using string literal notation.

44
00:03:16,840 --> 00:03:30,790
The title is self-taught title and the author is self-taught author save that.

45
00:03:31,140 --> 00:03:35,930
OK so what I'm doing here is going to zoom out so you can see the whole picture.

46
00:03:36,020 --> 00:03:41,210
I have the special method underscore underscore R E P R underscore underscore.

47
00:03:41,390 --> 00:03:47,480
And what's going to happen is when you called print on my book print is going to ask hey do you have

48
00:03:47,480 --> 00:03:50,570
a string representation of this book object I can use.

49
00:03:50,570 --> 00:03:56,360
And then this special method is going to say yeah no problem here's the representation and it's going

50
00:03:56,360 --> 00:04:00,800
to pasand the string and the string is using formatting to inject.

51
00:04:00,800 --> 00:04:06,500
Self-taught title the title provided appear and self the author the author provided appear.

52
00:04:06,500 --> 00:04:09,570
So now let's save this and try printing out our book again.

53
00:04:10,700 --> 00:04:15,990
And now when I print out my book object I get title is python rocks author Jose.

54
00:04:16,280 --> 00:04:20,840
So it's totally up to you what you want the actual representation of your object to be when you print

55
00:04:20,840 --> 00:04:21,330
it out.

56
00:04:21,560 --> 00:04:28,160
All you need to do is call this special method underscore underscore our EPR underscore underscore self

57
00:04:28,610 --> 00:04:31,830
and then return whatever string you want to be printed out.

58
00:04:31,940 --> 00:04:34,310
Again we're returning it not printing it here.

59
00:04:34,430 --> 00:04:35,150
You print it out.

60
00:04:35,180 --> 00:04:41,110
When you actually call that function just to make it very clear what's happening here I'll say print

61
00:04:41,110 --> 00:04:41,660
out.

62
00:04:43,100 --> 00:04:45,420
Yep actually returning.

63
00:04:45,770 --> 00:04:52,640
So in saying F are EPR return up and I want to print out my book for with to run this again I'll just

64
00:04:52,640 --> 00:04:57,590
say up and I'll see you up for any book no matter what title is what the author is and what the number

65
00:04:57,590 --> 00:04:58,520
of pages are.

66
00:04:58,850 --> 00:05:03,710
So it probably makes more sense to go for our original thing which was inject self-taught title and

67
00:05:03,800 --> 00:05:05,000
author.

68
00:05:05,120 --> 00:05:10,100
Again all we're doing here is when you print out the book object it's going to look down and say what's

69
00:05:10,100 --> 00:05:12,620
the representation provided.

70
00:05:12,640 --> 00:05:15,570
Now let's try another function that's built in.

71
00:05:15,800 --> 00:05:25,500
Let's see if we can check the length of the book and we'll say Lankes book is equal to length of my

72
00:05:25,500 --> 00:05:26,060
book.

73
00:05:26,370 --> 00:05:30,940
And then at the very end I'll just print out the length of book.

74
00:05:30,970 --> 00:05:33,400
Now let's see what happens if we actually try to run this.

75
00:05:33,760 --> 00:05:37,330
You'll get an error and it says object of type book has no length.

76
00:05:37,420 --> 00:05:38,800
And that actually makes sense.

77
00:05:38,980 --> 00:05:41,960
What should Python use to represent the length of your book.

78
00:05:41,980 --> 00:05:46,810
You haven't really told it so it can't just go in and guess because all it knows is that a book has

79
00:05:46,810 --> 00:05:48,680
a title author and pages.

80
00:05:48,790 --> 00:05:52,700
It's up to you to decide what the representation of the length should be.

81
00:05:53,570 --> 00:05:56,640
In this case we can use a special method to do that.

82
00:05:56,690 --> 00:06:00,950
We'll say the F and the method are related to the length function as underscore underscore.

83
00:06:01,170 --> 00:06:06,400
L e n underscore underscore.

84
00:06:06,620 --> 00:06:11,870
So this special length method is going to allow you to decide what you should return when you call length

85
00:06:12,050 --> 00:06:13,610
on your actual object.

86
00:06:13,730 --> 00:06:19,630
So this case let's go ahead and return the number of pages we'll say self-taught pages.

87
00:06:19,640 --> 00:06:26,120
Save that and now when I run this again it's going to return back the length of the book is 250.

88
00:06:26,150 --> 00:06:28,580
The number of pages I provided here.

89
00:06:28,580 --> 00:06:32,920
Again these are the two special methods we're going to be using throughout the course for working class.

90
00:06:33,110 --> 00:06:35,100
The string representation of an object.

91
00:06:35,130 --> 00:06:38,680
So that's going to be really useful when you want to print things out to get an idea of what they are

92
00:06:39,080 --> 00:06:44,860
and the length of an object that's going to be useful in order to interact with the length function.

93
00:06:45,020 --> 00:06:47,470
And those are really the only two special methods we need to know.

94
00:06:47,720 --> 00:06:52,790
Now you'll notice as you begin to type the F underscore underscore there's actually a ton of special

95
00:06:52,790 --> 00:06:54,290
methods available for you here.

96
00:06:54,440 --> 00:06:57,510
And if you have some sort of Python autocomplete you can get the whole list.

97
00:06:57,710 --> 00:07:00,190
But in our case we really don't need to use all.

98
00:07:00,230 --> 00:07:05,030
In fact mainly the one we're going to be using is this representation for a string to actually prints

99
00:07:05,090 --> 00:07:07,280
out our own objects.

100
00:07:07,310 --> 00:07:11,980
OK the last thing I want to mention is often in other Python courses including my own.

101
00:07:11,990 --> 00:07:19,400
You've probably seen our EPR shown as as TR So this will bouffe work for our use cases but since the

102
00:07:19,620 --> 00:07:25,670
Flast documentation tends to use our EPR for its actual string representation we're going to use that

103
00:07:25,670 --> 00:07:27,170
method throughout the course.

104
00:07:27,170 --> 00:07:30,790
Now if you're wondering what's the actual difference between SDR and our EPR.

105
00:07:30,920 --> 00:07:33,220
You can do a simple Google search for that.

106
00:07:33,380 --> 00:07:37,910
So say difference between underscore underscores TR and underscore underscore our EPR.

107
00:07:37,910 --> 00:07:42,140
This discussion is kind of advanced and it's outside the scope of this particular course.

108
00:07:42,260 --> 00:07:45,710
But you'll notice that there's actually lots and lots of posts because it's actually a really common

109
00:07:45,710 --> 00:07:46,290
question.

110
00:07:46,490 --> 00:07:52,760
But I would say for our level of discussion it's a to advance for us to really be able to see the differences

111
00:07:52,760 --> 00:07:55,940
between them and our cases we can actually use both.

112
00:07:56,030 --> 00:07:58,010
So you can check out the full discussion here.

113
00:07:58,010 --> 00:08:02,170
It's kind of a long discussion and it's also a pretty advanced discussion so just keep that in mind.

114
00:08:02,390 --> 00:08:08,570
If you're wondering what the differences for practical purposes there is none for our use cases.

115
00:08:08,630 --> 00:08:13,430
OK so those are all the special methods we need to know just how to print out our own objects and how

116
00:08:13,430 --> 00:08:15,460
to check the length of our own objects.

117
00:08:15,530 --> 00:08:19,900
Coming up next we're ancestor new skills of object oriented programming through a project.

118
00:08:19,910 --> 00:08:20,480
We'll see you there.
