WEBVTT

1
00:00:00.120 --> 00:00:01.690
<v ->Hi guys and welcome back,</v>

2
00:00:01.690 --> 00:00:04.670
in this video we're going to learn about two methods,

3
00:00:04.670 --> 00:00:07.870
the str and the repr methods.

4
00:00:07.870 --> 00:00:10.660
In Python methods with two underscores on each side

5
00:00:10.660 --> 00:00:13.920
are special methods also called magic methods at times,

6
00:00:13.920 --> 00:00:17.950
because Python will call them for you in some situations.

7
00:00:17.950 --> 00:00:20.540
For example when you do person,

8
00:00:20.540 --> 00:00:22.400
Python calls the init method for you,

9
00:00:22.400 --> 00:00:23.560
even though you didn't say

10
00:00:23.560 --> 00:00:26.810
Python dot underscore, underscore init, you know.

11
00:00:26.810 --> 00:00:28.700
So that is why it is a magic method,

12
00:00:28.700 --> 00:00:30.930
you don't have to call it yourself,

13
00:00:30.930 --> 00:00:33.540
Python can do it for you at times.

14
00:00:33.540 --> 00:00:38.540
So let's create a person that I'm gonna call Bob here

15
00:00:39.000 --> 00:00:41.150
and he's gonna be 35.

16
00:00:41.150 --> 00:00:46.040
Here we're creating a person object by calling the class,

17
00:00:46.040 --> 00:00:48.720
then Python calls the underscore, underscore,

18
00:00:48.720 --> 00:00:50.420
init, underscore, underscore method.

19
00:00:50.420 --> 00:00:54.680
Gives us a new empty object that we're calling self,

20
00:00:54.680 --> 00:00:55.940
and then we're setting the name

21
00:00:55.940 --> 00:00:59.690
and the age properties of that object to be our parameters

22
00:00:59.690 --> 00:01:01.600
that are arguments here,

23
00:01:01.600 --> 00:01:06.600
so Bob and 35 will be stored in Bob's name and age.

24
00:01:07.150 --> 00:01:10.950
Now if we print Bob then you may see something

25
00:01:10.950 --> 00:01:14.030
a little bit weird, which is this.

26
00:01:14.030 --> 00:01:17.030
That's less than, underscore, underscore, main,

27
00:01:17.030 --> 00:01:18.840
underscore, underscore, dot, person,

28
00:01:18.840 --> 00:01:22.240
object, at, a bunch of numbers.

29
00:01:22.240 --> 00:01:23.073
Greater than.

30
00:01:23.073 --> 00:01:24.360
So what does this mean?

31
00:01:24.360 --> 00:01:26.480
What python is doing at this point is

32
00:01:26.480 --> 00:01:28.210
when you've told it to print Bob,

33
00:01:28.210 --> 00:01:32.050
Python is going to print out a string

34
00:01:32.050 --> 00:01:35.740
representing the Bob objects that we've created.

35
00:01:35.740 --> 00:01:38.030
And the reason why Python is printing out

36
00:01:38.030 --> 00:01:39.930
a representation of the object,

37
00:01:39.930 --> 00:01:42.270
is because you can't really print an object,

38
00:01:42.270 --> 00:01:44.980
you have to print a string, some characters

39
00:01:44.980 --> 00:01:46.430
and numbers and so on.

40
00:01:46.430 --> 00:01:49.940
So Python is saying that this string

41
00:01:49.940 --> 00:01:54.790
that it printed out is what it thinks represents our object,

42
00:01:54.790 --> 00:01:57.370
this is a default string, and all objects

43
00:01:57.370 --> 00:02:01.090
and all classes you create will look something like that,

44
00:02:01.090 --> 00:02:03.110
unless you change it yourself.

45
00:02:03.110 --> 00:02:07.720
So the purpose of printing out an object can be to fold.

46
00:02:07.720 --> 00:02:10.390
The first purpose can be to return

47
00:02:10.390 --> 00:02:14.700
a nice, easy to read string so that in your applications

48
00:02:14.700 --> 00:02:16.270
you can print out an object

49
00:02:16.270 --> 00:02:18.850
and your users can make use of that.

50
00:02:18.850 --> 00:02:22.850
For example if they wanted to know about this Bob person

51
00:02:22.850 --> 00:02:25.040
then potentially you could print out Bob

52
00:02:25.040 --> 00:02:27.420
and it could show them some nice information about Bob.

53
00:02:27.420 --> 00:02:29.840
The other reason to print out an object

54
00:02:29.840 --> 00:02:33.610
can be for programmers, you can print out an object

55
00:02:33.610 --> 00:02:35.850
so that you can see the objects information

56
00:02:35.850 --> 00:02:38.570
in case you want to recreate the object.

57
00:02:38.570 --> 00:02:40.360
Let's start off with the first option.

58
00:02:40.360 --> 00:02:44.880
We're going to modify Bob so that when we print him out

59
00:02:44.880 --> 00:02:46.880
we'll get some nice information

60
00:02:46.880 --> 00:02:50.180
that our users can make use of.

61
00:02:50.180 --> 00:02:54.800
The way we do that is we do def, underscore, underscore,

62
00:02:54.800 --> 00:02:57.490
str, underscore, underscore.

63
00:02:57.490 --> 00:03:00.230
Then of course the self parameter,

64
00:03:00.230 --> 00:03:03.250
and in we go to the Bobby.

65
00:03:03.250 --> 00:03:05.640
The underscore, underscore, str, underscore, underscore

66
00:03:05.640 --> 00:03:08.060
is a method, that is a magic method

67
00:03:08.060 --> 00:03:10.260
because it has the two underscores on each side,

68
00:03:10.260 --> 00:03:12.860
and this method gets called for you,

69
00:03:12.860 --> 00:03:16.600
when you want to turn your object into a string.

70
00:03:16.600 --> 00:03:18.977
Let me just return hello from here.

71
00:03:18.977 --> 00:03:21.093
Any now we're going to print Bob.

72
00:03:22.160 --> 00:03:24.650
Notice that we get hello out.

73
00:03:24.650 --> 00:03:25.979
Because what Python has done,

74
00:03:25.979 --> 00:03:28.840
is when you ask it to print an object,

75
00:03:28.840 --> 00:03:30.810
Python realises that really what you want

76
00:03:30.810 --> 00:03:33.520
is a string representation of that object since

77
00:03:33.520 --> 00:03:34.700
that's the only thing you can actually

78
00:03:34.700 --> 00:03:36.090
print out in a console.

79
00:03:36.090 --> 00:03:38.580
When you ask it to print an object it is going to

80
00:03:38.580 --> 00:03:42.910
turn that object into a string using the str method,

81
00:03:42.910 --> 00:03:44.830
and then it's going to print that.

82
00:03:44.830 --> 00:03:47.000
So instead of returning hello here,

83
00:03:47.000 --> 00:03:49.220
we could return something a bit nicer.

84
00:03:49.220 --> 00:03:51.950
Run about the goal, remember the goal

85
00:03:51.950 --> 00:03:53.860
when you're using the underscore, underscore,

86
00:03:53.860 --> 00:03:56.160
str, underscore, underscore method is to

87
00:03:56.160 --> 00:04:01.160
print out a nice, easy to read string for users.

88
00:04:01.360 --> 00:04:04.570
So we're going to print something like this,

89
00:04:04.570 --> 00:04:09.570
person, self, dot name, self dot age, years old.

90
00:04:10.660 --> 00:04:12.980
Now if we run this code again,

91
00:04:12.980 --> 00:04:16.260
you see that we get person Bob, 35 years old.

92
00:04:16.260 --> 00:04:18.130
You can remove the person if you'd like.

93
00:04:18.130 --> 00:04:22.400
So as well as the str method, you have another one

94
00:04:22.400 --> 00:04:25.490
called the repr method.

95
00:04:25.490 --> 00:04:28.960
The goal of the repr method is to be unambiguous,

96
00:04:28.960 --> 00:04:31.888
and if possible it should return a string

97
00:04:31.888 --> 00:04:36.600
that allows us to recreate the original object very easily.

98
00:04:36.600 --> 00:04:41.600
So I'm going to return person, self dot name, self dot age.

99
00:04:42.540 --> 00:04:43.373
Like that.

100
00:04:43.373 --> 00:04:45.450
Notice that I'm putting less than and greater than symbols

101
00:04:45.450 --> 00:04:48.970
that is just for looks, it is to tell programmers

102
00:04:48.970 --> 00:04:50.730
that read this string here

103
00:04:50.730 --> 00:04:53.060
that we are printing out an object.

104
00:04:53.060 --> 00:04:55.500
Now if we press play, you can see

105
00:04:55.500 --> 00:04:59.260
that the str methods output is still printed out.

106
00:04:59.260 --> 00:05:02.690
That is because by default when you print out an object

107
00:05:02.690 --> 00:05:05.840
Python assumes that you wanna show it to your users.

108
00:05:05.840 --> 00:05:08.100
You don't want to show the output of the repr method,

109
00:05:08.100 --> 00:05:10.840
you wanna show the output of the str method.

110
00:05:10.840 --> 00:05:13.800
The repr method is used in a few other places

111
00:05:13.800 --> 00:05:15.520
such as the Python debugger

112
00:05:15.520 --> 00:05:17.381
that we will learn about later on.

113
00:05:17.381 --> 00:05:19.362
But usually the str method is used

114
00:05:19.362 --> 00:05:23.010
so if you wanna make sure to test out your repr method,

115
00:05:23.010 --> 00:05:24.240
just comment this one out,

116
00:05:24.240 --> 00:05:26.370
put a couple of hash symbols in front of it

117
00:05:26.370 --> 00:05:27.700
and then press play.

118
00:05:27.700 --> 00:05:31.980
And then you see that you get person, Bob, 35.

119
00:05:31.980 --> 00:05:35.530
And if you really wanted to you can put the quotes in there,

120
00:05:35.530 --> 00:05:38.950
so that when you press play you out string back,

121
00:05:38.950 --> 00:05:39.990
that's even better.

122
00:05:39.990 --> 00:05:41.680
So we've learned about two methods,

123
00:05:41.680 --> 00:05:44.210
that you can implement in your classes,

124
00:05:44.210 --> 00:05:47.230
the str and the repr methods.

125
00:05:47.230 --> 00:05:49.400
The str method is called when you wanna

126
00:05:49.400 --> 00:05:51.650
turn your object into a string

127
00:05:51.650 --> 00:05:52.780
such as when printing it out

128
00:05:52.780 --> 00:05:55.790
or even when doing str of Bob

129
00:05:55.790 --> 00:05:57.870
as we have learned previously with integers.

130
00:05:57.870 --> 00:06:00.280
The repr method is used to print out

131
00:06:00.280 --> 00:06:02.870
an unambiguous representation of an abject

132
00:06:02.870 --> 00:06:05.920
so that you can use that to recreate the object for example,

133
00:06:05.920 --> 00:06:09.330
if you were a programmer looking at that output.

134
00:06:09.330 --> 00:06:11.780
Usually you'll want to implement the str method,

135
00:06:11.780 --> 00:06:14.980
but implementing the repr method can also be quite nice.

136
00:06:14.980 --> 00:06:17.140
And remember you don't have to use these two,

137
00:06:17.140 --> 00:06:20.070
they're only used for representing an object

138
00:06:20.070 --> 00:06:22.130
as a string but you don't have to do that,

139
00:06:22.130 --> 00:06:23.180
if you don't want.

140
00:06:23.180 --> 00:06:24.100
That's it for this video,

141
00:06:24.100 --> 00:06:26.250
thank you for joining me and i'll see you--

