1
1

00:00:00,120  -->  00:00:01,690
<v ->Hi guys and welcome back,</v>
2

2

00:00:01,690  -->  00:00:04,670
in this video we're going to learn about two methods,
3

3

00:00:04,670  -->  00:00:07,870
the str and the repr methods.
4

4

00:00:07,870  -->  00:00:10,660
In Python methods with two underscores on each side
5

5

00:00:10,660  -->  00:00:13,920
are special methods also called magic methods at times,
6

6

00:00:13,920  -->  00:00:17,950
because Python will call them for you in some situations.
7

7

00:00:17,950  -->  00:00:20,540
For example when you do person,
8

8

00:00:20,540  -->  00:00:22,400
Python calls the init method for you,
9

9

00:00:22,400  -->  00:00:23,560
even though you didn't say
10

10

00:00:23,560  -->  00:00:26,810
Python dot underscore, underscore init, you know.
11

11

00:00:26,810  -->  00:00:28,700
So that is why it is a magic method,
12

12

00:00:28,700  -->  00:00:30,930
you don't have to call it yourself,
13

13

00:00:30,930  -->  00:00:33,540
Python can do it for you at times.
14

14

00:00:33,540  -->  00:00:38,540
So let's create a person that I'm gonna call Bob here
15

15

00:00:39,000  -->  00:00:41,150
and he's gonna be 35.
16

16

00:00:41,150  -->  00:00:46,040
Here we're creating a person object by calling the class,
17

17

00:00:46,040  -->  00:00:48,720
then Python calls the underscore, underscore,
18

18

00:00:48,720  -->  00:00:50,420
init, underscore, underscore method.
19

19

00:00:50,420  -->  00:00:54,680
Gives us a new empty object that we're calling self,
20

20

00:00:54,680  -->  00:00:55,940
and then we're setting the name
21

21

00:00:55,940  -->  00:00:59,690
and the age properties of that object to be our parameters
22

22

00:00:59,690  -->  00:01:01,600
that are arguments here,
23

23

00:01:01,600  -->  00:01:06,600
so Bob and 35 will be stored in Bob's name and age.
24

24

00:01:07,150  -->  00:01:10,950
Now if we print Bob then you may see something
25

25

00:01:10,950  -->  00:01:14,030
a little bit weird, which is this.
26

26

00:01:14,030  -->  00:01:17,030
That's less than, underscore, underscore, main,
27

27

00:01:17,030  -->  00:01:18,840
underscore, underscore, dot, person,
28

28

00:01:18,840  -->  00:01:22,240
object, at, a bunch of numbers.
29

29

00:01:22,240  -->  00:01:23,073
Greater than.
30

30

00:01:23,073  -->  00:01:24,360
So what does this mean?
31

31

00:01:24,360  -->  00:01:26,480
What python is doing at this point is
32

32

00:01:26,480  -->  00:01:28,210
when you've told it to print Bob,
33

33

00:01:28,210  -->  00:01:32,050
Python is going to print out a string
34

34

00:01:32,050  -->  00:01:35,740
representing the Bob objects that we've created.
35

35

00:01:35,740  -->  00:01:38,030
And the reason why Python is printing out
36

36

00:01:38,030  -->  00:01:39,930
a representation of the object,
37

37

00:01:39,930  -->  00:01:42,270
is because you can't really print an object,
38

38

00:01:42,270  -->  00:01:44,980
you have to print a string, some characters
39

39

00:01:44,980  -->  00:01:46,430
and numbers and so on.
40

40

00:01:46,430  -->  00:01:49,940
So Python is saying that this string
41

41

00:01:49,940  -->  00:01:54,790
that it printed out is what it thinks represents our object,
42

42

00:01:54,790  -->  00:01:57,370
this is a default string, and all objects
43

43

00:01:57,370  -->  00:02:01,090
and all classes you create will look something like that,
44

44

00:02:01,090  -->  00:02:03,110
unless you change it yourself.
45

45

00:02:03,110  -->  00:02:07,720
So the purpose of printing out an object can be to fold.
46

46

00:02:07,720  -->  00:02:10,390
The first purpose can be to return
47

47

00:02:10,390  -->  00:02:14,700
a nice, easy to read string so that in your applications
48

48

00:02:14,700  -->  00:02:16,270
you can print out an object
49

49

00:02:16,270  -->  00:02:18,850
and your users can make use of that.
50

50

00:02:18,850  -->  00:02:22,850
For example if they wanted to know about this Bob person
51

51

00:02:22,850  -->  00:02:25,040
then potentially you could print out Bob
52

52

00:02:25,040  -->  00:02:27,420
and it could show them some nice information about Bob.
53

53

00:02:27,420  -->  00:02:29,840
The other reason to print out an object
54

54

00:02:29,840  -->  00:02:33,610
can be for programmers, you can print out an object
55

55

00:02:33,610  -->  00:02:35,850
so that you can see the objects information
56

56

00:02:35,850  -->  00:02:38,570
in case you want to recreate the object.
57

57

00:02:38,570  -->  00:02:40,360
Let's start off with the first option.
58

58

00:02:40,360  -->  00:02:44,880
We're going to modify Bob so that when we print him out
59

59

00:02:44,880  -->  00:02:46,880
we'll get some nice information
60

60

00:02:46,880  -->  00:02:50,180
that our users can make use of.
61

61

00:02:50,180  -->  00:02:54,800
The way we do that is we do def, underscore, underscore,
62

62

00:02:54,800  -->  00:02:57,490
str, underscore, underscore.
63

63

00:02:57,490  -->  00:03:00,230
Then of course the self parameter,
64

64

00:03:00,230  -->  00:03:03,250
and in we go to the Bobby.
65

65

00:03:03,250  -->  00:03:05,640
The underscore, underscore, str, underscore, underscore
66

66

00:03:05,640  -->  00:03:08,060
is a method, that is a magic method
67

67

00:03:08,060  -->  00:03:10,260
because it has the two underscores on each side,
68

68

00:03:10,260  -->  00:03:12,860
and this method gets called for you,
69

69

00:03:12,860  -->  00:03:16,600
when you want to turn your object into a string.
70

70

00:03:16,600  -->  00:03:18,977
Let me just return hello from here.
71

71

00:03:18,977  -->  00:03:21,093
Any now we're going to print Bob.
72

72

00:03:22,160  -->  00:03:24,650
Notice that we get hello out.
73

73

00:03:24,650  -->  00:03:25,979
Because what Python has done,
74

74

00:03:25,979  -->  00:03:28,840
is when you ask it to print an object,
75

75

00:03:28,840  -->  00:03:30,810
Python realises that really what you want
76

76

00:03:30,810  -->  00:03:33,520
is a string representation of that object since
77

77

00:03:33,520  -->  00:03:34,700
that's the only thing you can actually
78

78

00:03:34,700  -->  00:03:36,090
print out in a console.
79

79

00:03:36,090  -->  00:03:38,580
When you ask it to print an object it is going to
80

80

00:03:38,580  -->  00:03:42,910
turn that object into a string using the str method,
81

81

00:03:42,910  -->  00:03:44,830
and then it's going to print that.
82

82

00:03:44,830  -->  00:03:47,000
So instead of returning hello here,
83

83

00:03:47,000  -->  00:03:49,220
we could return something a bit nicer.
84

84

00:03:49,220  -->  00:03:51,950
Run about the goal, remember the goal
85

85

00:03:51,950  -->  00:03:53,860
when you're using the underscore, underscore,
86

86

00:03:53,860  -->  00:03:56,160
str, underscore, underscore method is to
87

87

00:03:56,160  -->  00:04:01,160
print out a nice, easy to read string for users.
88

88

00:04:01,360  -->  00:04:04,570
So we're going to print something like this,
89

89

00:04:04,570  -->  00:04:09,570
person, self, dot name, self dot age, years old.
90

90

00:04:10,660  -->  00:04:12,980
Now if we run this code again,
91

91

00:04:12,980  -->  00:04:16,260
you see that we get person Bob, 35 years old.
92

92

00:04:16,260  -->  00:04:18,130
You can remove the person if you'd like.
93

93

00:04:18,130  -->  00:04:22,400
So as well as the str method, you have another one
94

94

00:04:22,400  -->  00:04:25,490
called the repr method.
95

95

00:04:25,490  -->  00:04:28,960
The goal of the repr method is to be unambiguous,
96

96

00:04:28,960  -->  00:04:31,888
and if possible it should return a string
97

97

00:04:31,888  -->  00:04:36,600
that allows us to recreate the original object very easily.
98

98

00:04:36,600  -->  00:04:41,600
So I'm going to return person, self dot name, self dot age.
99

99

00:04:42,540  -->  00:04:43,373
Like that.
100

100

00:04:43,373  -->  00:04:45,450
Notice that I'm putting less than and greater than symbols
101

101

00:04:45,450  -->  00:04:48,970
that is just for looks, it is to tell programmers
102

102

00:04:48,970  -->  00:04:50,730
that read this string here
103

103

00:04:50,730  -->  00:04:53,060
that we are printing out an object.
104

104

00:04:53,060  -->  00:04:55,500
Now if we press play, you can see
105

105

00:04:55,500  -->  00:04:59,260
that the str methods output is still printed out.
106

106

00:04:59,260  -->  00:05:02,690
That is because by default when you print out an object
107

107

00:05:02,690  -->  00:05:05,840
Python assumes that you wanna show it to your users.
108

108

00:05:05,840  -->  00:05:08,100
You don't want to show the output of the repr method,
109

109

00:05:08,100  -->  00:05:10,840
you wanna show the output of the str method.
110

110

00:05:10,840  -->  00:05:13,800
The repr method is used in a few other places
111

111

00:05:13,800  -->  00:05:15,520
such as the Python debugger
112

112

00:05:15,520  -->  00:05:17,381
that we will learn about later on.
113

113

00:05:17,381  -->  00:05:19,362
But usually the str method is used
114

114

00:05:19,362  -->  00:05:23,010
so if you wanna make sure to test out your repr method,
115

115

00:05:23,010  -->  00:05:24,240
just comment this one out,
116

116

00:05:24,240  -->  00:05:26,370
put a couple of hash symbols in front of it
117

117

00:05:26,370  -->  00:05:27,700
and then press play.
118

118

00:05:27,700  -->  00:05:31,980
And then you see that you get person, Bob, 35.
119

119

00:05:31,980  -->  00:05:35,530
And if you really wanted to you can put the quotes in there,
120

120

00:05:35,530  -->  00:05:38,950
so that when you press play you out string back,
121

121

00:05:38,950  -->  00:05:39,990
that's even better.
122

122

00:05:39,990  -->  00:05:41,680
So we've learned about two methods,
123

123

00:05:41,680  -->  00:05:44,210
that you can implement in your classes,
124

124

00:05:44,210  -->  00:05:47,230
the str and the repr methods.
125

125

00:05:47,230  -->  00:05:49,400
The str method is called when you wanna
126

126

00:05:49,400  -->  00:05:51,650
turn your object into a string
127

127

00:05:51,650  -->  00:05:52,780
such as when printing it out
128

128

00:05:52,780  -->  00:05:55,790
or even when doing str of Bob
129

129

00:05:55,790  -->  00:05:57,870
as we have learned previously with integers.
130

130

00:05:57,870  -->  00:06:00,280
The repr method is used to print out
131

131

00:06:00,280  -->  00:06:02,870
an unambiguous representation of an abject
132

132

00:06:02,870  -->  00:06:05,920
so that you can use that to recreate the object for example,
133

133

00:06:05,920  -->  00:06:09,330
if you were a programmer looking at that output.
134

134

00:06:09,330  -->  00:06:11,780
Usually you'll want to implement the str method,
135

135

00:06:11,780  -->  00:06:14,980
but implementing the repr method can also be quite nice.
136

136

00:06:14,980  -->  00:06:17,140
And remember you don't have to use these two,
137

137

00:06:17,140  -->  00:06:20,070
they're only used for representing an object
138

138

00:06:20,070  -->  00:06:22,130
as a string but you don't have to do that,
139

139

00:06:22,130  -->  00:06:23,180
if you don't want.
140

140

00:06:23,180  -->  00:06:24,100
That's it for this video,
141

141

00:06:24,100  -->  00:06:26,250
thank you for joining me and i'll see you--
