1
00:00:05,190 --> 00:00:09,840
Welcome back, everyone, to this last lecture on our discussion of object oriented programming.

2
00:00:10,080 --> 00:00:14,430
Well, we're going to be discussing what are known as special methods, also sometimes called thunder

3
00:00:14,430 --> 00:00:19,740
methods, because of the fact that they used to double underscores on each side of the method call.

4
00:00:20,130 --> 00:00:21,450
Let's explore this further.

5
00:00:21,450 --> 00:00:22,290
Are some examples.

6
00:00:22,680 --> 00:00:24,930
OK, here I am inside a Python file.

7
00:00:25,380 --> 00:00:30,510
And remember that Python itself has special, built in predefined functions.

8
00:00:30,900 --> 00:00:34,860
And a really simple example of that is the fact that you can print out objects.

9
00:00:35,010 --> 00:00:36,690
So print is a built in function.

10
00:00:37,080 --> 00:00:39,990
There's also the ability to check in the length of an object.

11
00:00:40,170 --> 00:00:42,270
That's also a special built in function.

12
00:00:42,690 --> 00:00:47,070
And you'll notice that Python objects can then interact with these built in functions.

13
00:00:47,490 --> 00:00:53,760
For example, if I create a list, my list one two three, I could say print my list.

14
00:00:54,180 --> 00:00:58,590
I could also do something like print the length of my list.

15
00:00:59,070 --> 00:01:04,620
And if I save those changes and run this file notice, I get back the list and then the fact that there's

16
00:01:04,620 --> 00:01:07,140
an integer amount of items in it, like three.

17
00:01:07,830 --> 00:01:14,340
Now the question arises, if we have created our own user defined classes or python objects, how would

18
00:01:14,340 --> 00:01:18,240
Python know what to actually print when you print a user defined object?

19
00:01:18,660 --> 00:01:21,720
How does Python know what the length of a user defined object is?

20
00:01:22,230 --> 00:01:28,170
Let's show a really simple example by creating our own class for an object we call a book.

21
00:01:28,560 --> 00:01:33,630
Maybe later on, we're developing a bookstore web application, and I've created my own custom object

22
00:01:33,630 --> 00:01:36,810
called Book in order to help organize and define what a book is.

23
00:01:37,440 --> 00:01:45,330
So upon instantiating a book, I call the init method here I pass in self, and let's imagine a book

24
00:01:45,330 --> 00:01:46,290
has a title.

25
00:01:47,650 --> 00:01:51,850
Has an author and also has a number of pages associated with it.

26
00:01:52,240 --> 00:01:58,540
And so these are attributes that when we maybe register a new book into our little Amazon online store,

27
00:01:58,750 --> 00:02:02,980
then I register a title author and pages, maybe later on or else register a price or something like

28
00:02:02,980 --> 00:02:03,250
that.

29
00:02:03,760 --> 00:02:08,020
So we're going to say self-built title is equal to the title passed in self.

30
00:02:08,050 --> 00:02:14,140
The author is equal to the author Peston and then self-taught pages is equal to the pages passed then.

31
00:02:15,100 --> 00:02:19,720
And what I'm going to do is create an instance of my book and let's create a new book.

32
00:02:20,290 --> 00:02:23,410
This book's title could be Python Rocks.

33
00:02:24,100 --> 00:02:26,030
The author could be me, Josie.

34
00:02:26,710 --> 00:02:29,680
And let's say the pages is one hundred and twenty pages long.

35
00:02:30,640 --> 00:02:34,450
Now the question is what happens if I ask Python to print out my book?

36
00:02:34,780 --> 00:02:36,370
We just saw it work with my list.

37
00:02:36,550 --> 00:02:38,200
So let's see what happens if I say prints.

38
00:02:39,180 --> 00:02:46,620
My book, so I say this and that one, I run this, I get back this kind of ugly default print, which

39
00:02:46,620 --> 00:02:49,770
is, hey, inside your main script, which is example that pie.

40
00:02:49,770 --> 00:02:53,790
Here you've created a book object at this location in memory.

41
00:02:54,300 --> 00:03:00,270
This is not super useful for someone who's trying to maybe debug code or display something to a client's

42
00:03:00,270 --> 00:03:02,880
output, or they're just trying to print out my book.

43
00:03:03,330 --> 00:03:08,730
So clearly there's issues here on what Python can actually do for us just by default.

44
00:03:09,120 --> 00:03:14,010
And then if I were to do something like print out the length of my book, we definitely haven't told

45
00:03:14,010 --> 00:03:16,290
Python what the length of a book is.

46
00:03:16,770 --> 00:03:20,790
The python by itself is not going to understand that pages is probably the length.

47
00:03:21,150 --> 00:03:23,940
In fact, it's just going to give us an error here if we try to run this.

48
00:03:24,330 --> 00:03:29,520
It says, Hey, sorry, but the object of type book has no length because we haven't actually added

49
00:03:29,520 --> 00:03:33,450
in the special methods associated with these built in functions.

50
00:03:34,230 --> 00:03:40,170
You can think of special methods or thunder methods as essentially methods that are linked to built

51
00:03:40,200 --> 00:03:42,540
in functions or special calls within Python.

52
00:03:43,080 --> 00:03:49,080
So let's actually now show how we can create our own custom string representation of an object in order

53
00:03:49,080 --> 00:03:53,370
for it to be printed out, as well as our own custom length of an object for it to be printed out.

54
00:03:53,850 --> 00:03:58,530
We're going to start off with the simpler one, which is printing out an object.

55
00:03:58,950 --> 00:04:04,500
So by default, Python can print out an object by reporting it's an object at some location in memory.

56
00:04:04,920 --> 00:04:10,560
Typically, however, what we're going to want to do is have our own prints or string representation.

57
00:04:11,460 --> 00:04:17,399
So this method that's associated with print is underscore, underscore note the two sets of underscores

58
00:04:17,640 --> 00:04:20,130
and you can see there's actually a bunch of different methods here related to that.

59
00:04:20,459 --> 00:04:26,750
And then it's S.T.A.R. underscore underscore and we pass in self colon.

60
00:04:27,330 --> 00:04:33,000
And what this method does is whenever Python needs some sort of string representation of your object,

61
00:04:33,240 --> 00:04:37,650
whether it's using something like print formatting, like if string literals or is trying to print the

62
00:04:37,650 --> 00:04:39,810
object itself, it looks up.

63
00:04:40,050 --> 00:04:46,320
Does this actual object have a string representation defined by the user that it wants me to display?

64
00:04:47,100 --> 00:04:50,370
And in this case, remember, you're always going to return here.

65
00:04:50,610 --> 00:04:53,370
Don't get confused by the fact that it's connected to print.

66
00:04:53,580 --> 00:04:57,150
You do not use prints inside SDR methods.

67
00:04:57,420 --> 00:05:01,650
Instead, you're going to return the string representation you want for your book.

68
00:05:02,010 --> 00:05:04,350
And this is where you actually have a lot of flexibility.

69
00:05:04,650 --> 00:05:11,520
But maybe I just want to say the title and author for my book, so I'm going to return and f string

70
00:05:11,520 --> 00:05:12,090
literal here.

71
00:05:12,090 --> 00:05:15,810
That just says the title like self-taught.

72
00:05:17,350 --> 00:05:19,420
Title Curly Braces.

73
00:05:20,510 --> 00:05:22,550
Written by and then the author.

74
00:05:22,760 --> 00:05:25,220
Self-taught author.

75
00:05:25,850 --> 00:05:30,860
And now it's going to happen is every time Python needs a string representation of the object, it's

76
00:05:30,860 --> 00:05:37,100
going to ask for this method to be executed and return whatever that method happens to return.

77
00:05:37,340 --> 00:05:40,760
And then that's what it's going to actually print out when it prints out my book.

78
00:05:41,180 --> 00:05:47,690
So if you save these changes and now run this file instead of seeing this kind of ugly little thing

79
00:05:47,690 --> 00:05:53,240
that says, Hey, book object in this point of memory, I now get to see the title written by Jose.

80
00:05:53,450 --> 00:05:58,020
And again, you have a lot of flexibility over how you should report this or what you want to actually

81
00:05:58,070 --> 00:05:58,640
print it out.

82
00:05:58,910 --> 00:06:03,140
I just want to point out to fact that you have this string method available for you.

83
00:06:03,290 --> 00:06:08,330
Again, it's underscore, underscore, SDR, underscore, underscore, and this is your ability to return

84
00:06:08,480 --> 00:06:13,040
whatever string representation you want when you're printing out your user defined object.

85
00:06:13,310 --> 00:06:17,960
And we'll actually be using this a lot within Django because oftentimes we're going to be creating these

86
00:06:17,960 --> 00:06:23,600
unique models and databases that when we actually want to display to a user an instance of that particular

87
00:06:23,600 --> 00:06:28,520
data point, we'll probably have to customize what should actually be displayed or prints it out for

88
00:06:28,520 --> 00:06:29,030
the user.

89
00:06:29,750 --> 00:06:30,950
So that's the string method.

90
00:06:31,310 --> 00:06:35,630
The other one that's commonly used is the length method.

91
00:06:35,630 --> 00:06:41,120
So we say underscore, underscore, LPN, underscore, underscore and self.

92
00:06:41,540 --> 00:06:43,370
And this one is a little more strict.

93
00:06:43,700 --> 00:06:46,550
SDR should always be returning a string.

94
00:06:47,030 --> 00:06:49,760
LPN should always be returning an integer.

95
00:06:50,240 --> 00:06:55,100
So just keep that in mind, you can't actually return back a string or something like that has to be

96
00:06:55,100 --> 00:06:59,780
an integer, because that's the way Python thinks about the length of things as complete units.

97
00:07:00,200 --> 00:07:07,490
So what we're going to say here is I'm going to return, and if I wanted to, I could just return the

98
00:07:07,490 --> 00:07:10,850
number ninety nine whenever somebody asked me for the length of a book.

99
00:07:11,060 --> 00:07:15,230
Obviously, it's not super useful, but I do want to show you that it would technically work.

100
00:07:15,800 --> 00:07:21,230
So if I now say, Hey, print the length of my book here, I mean, certain that's a little bit so we

101
00:07:21,230 --> 00:07:21,770
can see that.

102
00:07:22,190 --> 00:07:25,790
Then it's going to say, OK, let me look up the length of this object and it's going to return back.

103
00:07:25,790 --> 00:07:26,360
Ninety nine.

104
00:07:26,930 --> 00:07:29,300
So when you run that, it returns back ninety nine.

105
00:07:29,750 --> 00:07:32,000
Clearly, we don't want this to be the same for every book.

106
00:07:32,240 --> 00:07:36,590
Probably makes more sense for it to return the number of pages, so we'll say.

107
00:07:37,770 --> 00:07:42,990
Self, that pages is going to be returned when you ask for the length of a book.

108
00:07:43,560 --> 00:07:48,870
And so now when I print the length of my book, it says, OK, it's 120, it's up to you.

109
00:07:48,870 --> 00:07:55,350
The context, the user and whoever is working with your Python file to see if just the number 120 actually

110
00:07:55,350 --> 00:07:56,130
makes sense.

111
00:07:56,430 --> 00:07:57,270
But keep that in mind.

112
00:07:57,510 --> 00:08:04,200
You have the ability to define your own string representations of objects and the length of your objects.

113
00:08:04,500 --> 00:08:08,820
And these are really the two special methods that we're going to be using quite a bit when we talk about

114
00:08:08,820 --> 00:08:11,310
creating our own databases and models within Django.

115
00:08:11,610 --> 00:08:14,790
We'll be storing things like books in our own little database.

116
00:08:14,790 --> 00:08:19,710
And then later on, maybe we want to display on the web page some string representation of the object,

117
00:08:20,010 --> 00:08:23,010
in which case it's really useful to actually show something like this.

118
00:08:23,980 --> 00:08:24,290
All right.

119
00:08:24,310 --> 00:08:25,420
That's it for special methods.

120
00:08:25,570 --> 00:08:26,590
I'll see you at the next lecture.

