1
00:00:01,170 --> 00:00:07,560
In this lecture, I will introduce you to a fundamental concept of object oriented programming, which

2
00:00:07,560 --> 00:00:09,900
is classes and objects.

3
00:00:10,650 --> 00:00:14,550
Now, first of all, let's try and define what is a class.

4
00:00:14,550 --> 00:00:22,140
And I'm looking here at the definition provided by the Python three documentation, the total documentation

5
00:00:22,140 --> 00:00:22,440
here.

6
00:00:22,890 --> 00:00:28,650
And it says that a class is a bundle of data and functionality.

7
00:00:29,040 --> 00:00:37,050
So think of classes as a programming construct that bundles together a number of functions and data

8
00:00:37,050 --> 00:00:44,290
on which those functions can operate when you want to use a class in your program.

9
00:00:44,310 --> 00:00:48,370
Then you need to create an instance of this class to make it real.

10
00:00:48,720 --> 00:00:55,740
Take it out of a blueprint and actually being a thing stored in memory of your computer and that is

11
00:00:55,740 --> 00:00:57,190
code an object.

12
00:00:57,570 --> 00:01:06,720
So when a class is defined in program, it doesn't do anything until it is instantiated into an object

13
00:01:06,900 --> 00:01:10,290
and can show you how that is done in Python in a moment.

14
00:01:10,950 --> 00:01:17,580
So this may seem confusing in the beginning, especially if you haven't worked with object oriented

15
00:01:17,580 --> 00:01:18,780
programming in the past.

16
00:01:19,020 --> 00:01:25,980
But I just want you to not worry about this at all at the moment and just think of a class as simply

17
00:01:25,980 --> 00:01:31,540
a piece of code that contains functions and associated data.

18
00:01:32,010 --> 00:01:40,200
So then we can create an instance of this class and be able to use its functions and its data.

19
00:01:41,160 --> 00:01:42,280
And to make this clear.

20
00:01:42,300 --> 00:01:44,780
Let's work on an example as always.

21
00:01:44,790 --> 00:01:51,570
So what we've got here is phony and I'm using the phony supplied python.

22
00:01:51,720 --> 00:01:56,300
But everything that I'm going to show you also works on Micro Python on the E.S.P 32.

23
00:01:57,180 --> 00:02:02,640
I'm going to start by creating a class of a calculator.

24
00:02:02,850 --> 00:02:10,500
So in previous lectures, we've been playing around with simple additions and then even in the previous

25
00:02:10,500 --> 00:02:14,200
lecture, we created a function.

26
00:02:14,220 --> 00:02:19,800
I'm just going to show you really quickly what it looks like, which is a function like this that we

27
00:02:19,800 --> 00:02:29,550
were then able to call by name and pass the numbers to it, and the function would execute the calculation

28
00:02:29,550 --> 00:02:31,020
and would give us the result back.

29
00:02:31,560 --> 00:02:39,210
Now, what we're going to do is we are going to create a calculator by bundling together.

30
00:02:39,390 --> 00:02:45,990
A couple of functions like this in my calculator is going to be very simple, is going to have the ability

31
00:02:45,990 --> 00:02:48,330
to add and to subtract.

32
00:02:49,530 --> 00:02:52,080
But we're going to do that as a class.

33
00:02:52,110 --> 00:02:59,680
So the class is going to be the collection of those two simple functions, add and subtract.

34
00:03:00,420 --> 00:03:04,530
So let's begin instead with a class Keywood.

35
00:03:05,070 --> 00:03:07,860
When we create a class, we use the class Keywood.

36
00:03:07,860 --> 00:03:14,130
When we want to create a function, we use the def definition Keywood.

37
00:03:14,580 --> 00:03:21,000
And it's going to be a calculator to start this with a capital C calculator.

38
00:03:23,040 --> 00:03:30,930
Classes don't receive parameters, so I'm going to finish with the colon symbol and then I'm going to

39
00:03:30,930 --> 00:03:33,150
define the two functions.

40
00:03:33,160 --> 00:03:34,860
The first one is going to be addition.

41
00:03:39,300 --> 00:03:49,500
For the parameters that appear inside a class, I always need to use the self.

42
00:03:51,860 --> 00:03:59,900
Keywood grew up here about sylph so self used to represent the instance of the class, so think of it

43
00:03:59,900 --> 00:04:07,040
as a key word that allows me to properly use references to local variables.

44
00:04:08,570 --> 00:04:15,080
Just one thing to remember, as you see in a moment, sylph is not really a parameter when I actually

45
00:04:15,080 --> 00:04:18,920
call the addition function from outside my class.

46
00:04:19,640 --> 00:04:24,860
I'm not going to count the self Keywood as a parameter.

47
00:04:24,860 --> 00:04:33,100
So this function as far as the outside world is concerned, only has two parameters for its finish with

48
00:04:33,110 --> 00:04:33,810
a definition.

49
00:04:33,830 --> 00:04:40,850
I'm just going to say here, return one class to keep it really simple.

50
00:04:41,390 --> 00:04:50,570
All right, then I'm going to create another function called Truxtun again, going to use a key word

51
00:04:50,570 --> 00:04:53,540
because I'm creating a function inside a class.

52
00:04:54,210 --> 00:04:56,570
It's going to be number one and number two.

53
00:04:58,510 --> 00:05:05,770
And the return statement will return the subtraction between number one and number two.

54
00:05:07,990 --> 00:05:08,630
And that's it.

55
00:05:08,920 --> 00:05:15,880
I now have completed the class definition, which doesn't have any local data, but it does have two

56
00:05:16,720 --> 00:05:21,300
class functions, addition and subtraction, and that is totally OK.

57
00:05:21,670 --> 00:05:28,360
Now I can use my new calculator class until I create an instance of this class.

58
00:05:28,870 --> 00:05:36,070
To create an instance of this class, I need to first create the variable and then call the name of

59
00:05:36,070 --> 00:05:36,730
the class.

60
00:05:38,490 --> 00:05:44,970
Open and close parenthesis, since there are no parameters to be passed into the calculator class when

61
00:05:44,970 --> 00:05:53,280
I created this, I show you later on we are going to be able to initialize my new instance of this class

62
00:05:53,670 --> 00:05:58,100
by passing parameters in what is called an initialize or a constructor.

63
00:05:58,530 --> 00:06:01,080
But we haven't created such a constructor yet.

64
00:06:01,080 --> 00:06:06,640
So I'm going to go with just empty parameters for the parentheses done.

65
00:06:06,660 --> 00:06:08,730
So now we have a calculator.

66
00:06:08,850 --> 00:06:16,080
The instance of this calculator is accessible via a variable for going to, say, a dot and hit the

67
00:06:16,080 --> 00:06:16,740
tab key.

68
00:06:16,740 --> 00:06:25,320
You'll see that now phoneme and code completion gives me access to the two available functions of this

69
00:06:25,320 --> 00:06:32,070
calculator that we want to add some just going to say addition and then pass the two parameters that

70
00:06:32,070 --> 00:06:33,870
the addition function is expecting.

71
00:06:34,290 --> 00:06:39,510
So let's say two and three enter and there is the answer coming back.

72
00:06:40,260 --> 00:06:46,800
I can use the subtraction next subtraction, let's say two and three again.

73
00:06:47,220 --> 00:06:49,080
And that gives me minus one.

74
00:06:49,710 --> 00:06:50,060
Perfect.

75
00:06:50,250 --> 00:06:50,530
All right.

76
00:06:50,550 --> 00:06:57,870
So this is your first class and this is your first instance of this class.

77
00:06:57,870 --> 00:06:59,740
And you can see nothing scary here.

78
00:06:59,740 --> 00:07:04,950
It's just a bundle in this case, a bundle of two simple functions.

79
00:07:06,880 --> 00:07:16,750
And the next thing that we can do is to redefine this calculator by using a constructor, and there

80
00:07:16,750 --> 00:07:21,620
is going to give us the opportunity to simplify the use of our calculator.

81
00:07:21,940 --> 00:07:28,990
So we'll be able to do something like this, will be able to create the calculator and initialize it

82
00:07:28,990 --> 00:07:30,040
with a couple of values.

83
00:07:30,070 --> 00:07:32,960
It's going to show you the end result first and then will be implemented.

84
00:07:33,490 --> 00:07:39,480
Let's say that we want a calculator that acts on numbers one and two.

85
00:07:39,490 --> 00:07:42,400
So we'll be able to initialize it like this.

86
00:07:42,400 --> 00:07:50,560
And we pass the initial values of our calculator inside the parenthesis parameters.

87
00:07:51,010 --> 00:07:58,030
And then what this allows us to do is to be able to simply call the addition function or the subtraction

88
00:07:58,030 --> 00:08:05,110
function without having to worry about passing parameters because the parameters are already set in

89
00:08:05,110 --> 00:08:10,660
the local variables of the instance of our calculator function.

90
00:08:11,060 --> 00:08:18,370
So we'll be able to simplify the user interface of our function like this, just initialize the instance

91
00:08:18,370 --> 00:08:21,520
and then just call the functions without any parameters.

92
00:08:22,420 --> 00:08:23,470
So how do we do this?

93
00:08:24,270 --> 00:08:25,000
Let's get into it.

94
00:08:26,020 --> 00:08:31,240
Would first create the definition of the calculator like this.

95
00:08:32,840 --> 00:08:40,610
And then we use a special function or function with a special name, I should say, which looks like

96
00:08:40,610 --> 00:08:50,720
this neat and it's kind of got to underscore symbols along each side of the init function name.

97
00:08:51,390 --> 00:08:56,480
And then again, the self keyword and number one.

98
00:08:57,110 --> 00:08:58,340
And number two.

99
00:08:59,780 --> 00:09:07,400
So what I'm saying here is that for the user to initialize this object, the user would need to pass

100
00:09:07,400 --> 00:09:09,620
in two numbers for the parameters.

101
00:09:10,970 --> 00:09:19,250
Now, what we're going to do with those numbers is to store them into local instance variables from

102
00:09:19,280 --> 00:09:25,580
using the self key word for not wanting to and.

103
00:09:27,430 --> 00:09:35,740
What I'm saying is that Python will take whatever value of typed into the first parameter of the constructor,

104
00:09:36,610 --> 00:09:44,200
which is number one, and then pass it into the calculator's object, local, number one, viable.

105
00:09:44,590 --> 00:09:46,740
And the same thing will happen with number two.

106
00:09:48,580 --> 00:09:49,230
All right.

107
00:09:49,240 --> 00:09:53,770
Now I'm going to define one back.

108
00:09:54,310 --> 00:09:57,130
I need to define my condition

109
00:10:00,700 --> 00:10:07,840
now because I have already defined the local variables for number one.

110
00:10:07,930 --> 00:10:13,100
And number two, I don't need to ask for any additional parameters from the user.

111
00:10:13,630 --> 00:10:16,720
So just going to self and that's it.

112
00:10:17,500 --> 00:10:27,850
And then for the return statement, I want to return the addition between the one local variable and

113
00:10:27,880 --> 00:10:30,250
known to the local variable.

114
00:10:30,760 --> 00:10:34,990
It said I to the last one is going to copy this one.

115
00:10:36,500 --> 00:10:37,530
So this time.

116
00:10:39,560 --> 00:10:45,650
And the return, you can probably guess what is going to be here, looks like this.

117
00:10:47,660 --> 00:10:57,140
All right, now let's create an instance from this calculator, so I am going to redefine that is going

118
00:10:57,140 --> 00:10:58,280
to be calculator.

119
00:11:02,590 --> 00:11:07,960
And the constructor allows me to put in two numbers, which makes them two in three.

120
00:11:08,530 --> 00:11:09,410
OK, done.

121
00:11:09,430 --> 00:11:11,440
Now I'm going to call in addition,

122
00:11:14,890 --> 00:11:17,830
without any parameters and as a result.

123
00:11:17,830 --> 00:11:18,670
So addition.

124
00:11:20,520 --> 00:11:26,580
Added together, number one, and number two, the local variables, and he gave me back the correct

125
00:11:26,580 --> 00:11:33,810
result, you can do the same thing for subtraction of the gross integer sort of cause.

126
00:11:33,810 --> 00:11:38,180
Now that we have a class, we can have as many instances as we want.

127
00:11:38,210 --> 00:11:44,670
Second, call for the calculator and make that, say, five and a nine.

128
00:11:45,810 --> 00:11:47,070
And now I've got.

129
00:11:50,050 --> 00:11:56,320
The ability to perform additional calculations in a separate calculator instance.

130
00:11:57,880 --> 00:12:02,350
Now, you might ask, what is it going to add two different numbers together?

131
00:12:02,680 --> 00:12:09,380
I've created the object A and initialized it with no one thing, two and three being three.

132
00:12:09,820 --> 00:12:14,400
But what if I want to make changes to those two values?

133
00:12:14,830 --> 00:12:22,420
So that's very easy, actually, because if you type in A and then dot and enter a sorry tab, the tab

134
00:12:22,420 --> 00:12:31,150
key for code completion, you see that Thony knows that there are two local variables in this object,

135
00:12:31,150 --> 00:12:31,750
number one.

136
00:12:31,750 --> 00:12:41,320
And number two, and you can use this notation now a dot number one in order to make a change to this

137
00:12:41,320 --> 00:12:48,240
particular instance variable so I can make a change and change that original one into a five.

138
00:12:48,880 --> 00:12:52,030
And now I can just make an addition again.

139
00:12:52,390 --> 00:13:01,110
And you see that the addition is now five plus the original three can also print out the current values

140
00:13:01,120 --> 00:13:08,380
of a number one and a number two.

141
00:13:08,890 --> 00:13:13,330
So you can also interrogate your object for each local variables.

142
00:13:14,590 --> 00:13:15,730
So I'm going to leave it at that.

143
00:13:16,360 --> 00:13:21,880
But, you know, at this point about objects and classes is more than enough compared to what you need

144
00:13:21,880 --> 00:13:27,850
in order to easily complete the exercises in the remainder of this course.

145
00:13:28,510 --> 00:13:33,970
But in the next lecture, there's one more thing that I want to talk to you about before moving on.

146
00:13:34,210 --> 00:13:41,800
And that is some of the most important differences between micro python and Python that need to be aware

147
00:13:41,800 --> 00:13:42,760
of going through.
