In this lecture, I will introduce you to a fundamental concept of object oriented programming, which
is classes and objects.
Now, first of all, let's try and define what is a class.
And I'm looking here at the definition provided by the Python three documentation, the total documentation
here.
And it says that a class is a bundle of data and functionality.
So think of classes as a programming construct that bundles together a number of functions and data
on which those functions can operate when you want to use a class in your program.
Then you need to create an instance of this class to make it real.
Take it out of a blueprint and actually being a thing stored in memory of your computer and that is
code an object.
So when a class is defined in program, it doesn't do anything until it is instantiated into an object
and can show you how that is done in Python in a moment.
So this may seem confusing in the beginning, especially if you haven't worked with object oriented
programming in the past.
But I just want you to not worry about this at all at the moment and just think of a class as simply
a piece of code that contains functions and associated data.
So then we can create an instance of this class and be able to use its functions and its data.
And to make this clear.
Let's work on an example as always.
So what we've got here is phony and I'm using the phony supplied python.
But everything that I'm going to show you also works on Micro Python on the E.S.P 32.
I'm going to start by creating a class of a calculator.
So in previous lectures, we've been playing around with simple additions and then even in the previous
lecture, we created a function.
I'm just going to show you really quickly what it looks like, which is a function like this that we
were then able to call by name and pass the numbers to it, and the function would execute the calculation
and would give us the result back.
Now, what we're going to do is we are going to create a calculator by bundling together.
A couple of functions like this in my calculator is going to be very simple, is going to have the ability
to add and to subtract.
But we're going to do that as a class.
So the class is going to be the collection of those two simple functions, add and subtract.
So let's begin instead with a class Keywood.
When we create a class, we use the class Keywood.
When we want to create a function, we use the def definition Keywood.
And it's going to be a calculator to start this with a capital C calculator.
Classes don't receive parameters, so I'm going to finish with the colon symbol and then I'm going to
define the two functions.
The first one is going to be addition.
For the parameters that appear inside a class, I always need to use the self.
Keywood grew up here about sylph so self used to represent the instance of the class, so think of it
as a key word that allows me to properly use references to local variables.
Just one thing to remember, as you see in a moment, sylph is not really a parameter when I actually
call the addition function from outside my class.
I'm not going to count the self Keywood as a parameter.
So this function as far as the outside world is concerned, only has two parameters for its finish with
a definition.
I'm just going to say here, return one class to keep it really simple.
All right, then I'm going to create another function called Truxtun again, going to use a key word
because I'm creating a function inside a class.
It's going to be number one and number two.
And the return statement will return the subtraction between number one and number two.
And that's it.
I now have completed the class definition, which doesn't have any local data, but it does have two
class functions, addition and subtraction, and that is totally OK.
Now I can use my new calculator class until I create an instance of this class.
To create an instance of this class, I need to first create the variable and then call the name of
the class.
Open and close parenthesis, since there are no parameters to be passed into the calculator class when
I created this, I show you later on we are going to be able to initialize my new instance of this class
by passing parameters in what is called an initialize or a constructor.
But we haven't created such a constructor yet.
So I'm going to go with just empty parameters for the parentheses done.
So now we have a calculator.
The instance of this calculator is accessible via a variable for going to, say, a dot and hit the
tab key.
You'll see that now phoneme and code completion gives me access to the two available functions of this
calculator that we want to add some just going to say addition and then pass the two parameters that
the addition function is expecting.
So let's say two and three enter and there is the answer coming back.
I can use the subtraction next subtraction, let's say two and three again.
And that gives me minus one.
Perfect.
All right.
So this is your first class and this is your first instance of this class.
And you can see nothing scary here.
It's just a bundle in this case, a bundle of two simple functions.
And the next thing that we can do is to redefine this calculator by using a constructor, and there
is going to give us the opportunity to simplify the use of our calculator.
So we'll be able to do something like this, will be able to create the calculator and initialize it
with a couple of values.
It's going to show you the end result first and then will be implemented.
Let's say that we want a calculator that acts on numbers one and two.
So we'll be able to initialize it like this.
And we pass the initial values of our calculator inside the parenthesis parameters.
And then what this allows us to do is to be able to simply call the addition function or the subtraction
function without having to worry about passing parameters because the parameters are already set in
the local variables of the instance of our calculator function.
So we'll be able to simplify the user interface of our function like this, just initialize the instance
and then just call the functions without any parameters.
So how do we do this?
Let's get into it.
Would first create the definition of the calculator like this.
And then we use a special function or function with a special name, I should say, which looks like
this neat and it's kind of got to underscore symbols along each side of the init function name.
And then again, the self keyword and number one.
And number two.
So what I'm saying here is that for the user to initialize this object, the user would need to pass
in two numbers for the parameters.
Now, what we're going to do with those numbers is to store them into local instance variables from
using the self key word for not wanting to and.
What I'm saying is that Python will take whatever value of typed into the first parameter of the constructor,
which is number one, and then pass it into the calculator's object, local, number one, viable.
And the same thing will happen with number two.
All right.
Now I'm going to define one back.
I need to define my condition
now because I have already defined the local variables for number one.
And number two, I don't need to ask for any additional parameters from the user.
So just going to self and that's it.
And then for the return statement, I want to return the addition between the one local variable and
known to the local variable.
It said I to the last one is going to copy this one.
So this time.
And the return, you can probably guess what is going to be here, looks like this.
All right, now let's create an instance from this calculator, so I am going to redefine that is going
to be calculator.
And the constructor allows me to put in two numbers, which makes them two in three.
OK, done.
Now I'm going to call in addition,
without any parameters and as a result.
So addition.
Added together, number one, and number two, the local variables, and he gave me back the correct
result, you can do the same thing for subtraction of the gross integer sort of cause.
Now that we have a class, we can have as many instances as we want.
Second, call for the calculator and make that, say, five and a nine.
And now I've got.
The ability to perform additional calculations in a separate calculator instance.
Now, you might ask, what is it going to add two different numbers together?
I've created the object A and initialized it with no one thing, two and three being three.
But what if I want to make changes to those two values?
So that's very easy, actually, because if you type in A and then dot and enter a sorry tab, the tab
key for code completion, you see that Thony knows that there are two local variables in this object,
number one.
And number two, and you can use this notation now a dot number one in order to make a change to this
particular instance variable so I can make a change and change that original one into a five.
And now I can just make an addition again.
And you see that the addition is now five plus the original three can also print out the current values
of a number one and a number two.
So you can also interrogate your object for each local variables.
So I'm going to leave it at that.
But, you know, at this point about objects and classes is more than enough compared to what you need
in order to easily complete the exercises in the remainder of this course.
But in the next lecture, there's one more thing that I want to talk to you about before moving on.
And that is some of the most important differences between micro python and Python that need to be aware
of going through.