Welcome back. So, last time we talked
about creating classes and again the way that you create
a class is you say class and then
the name of the class which usually starts with a capital letter.
So, I'll say class point.
I'm going to leave opening and closing parentheses here.
Then you usually declare constructor method which is underscore init, underscore.
Every method takes in an argument self.
In the case of point,
we'll call x and y,
the arguments of the constructor and
then we set the instance variables inside of the constructor.
So, I'll say self dot x and self dot y equals y.
In this lecture, I want to give some more detail
about what actually happens when we construct
in instance of this point class and how
Python actually searches for instance variables and methods.
So, we construct a new point,
I'll say one equals point and then we need to pass in values for x and y.
So, I'll say that x is equal to 10 and y is equal to 100 for point one.
So, let's suppose that we declared two methods.
Get x which returns
self dot x and get
y which returns self dot y.
When we declare these methods,
what Python does is it has some internal representation of the class itself.
Now, we've talked about the metaphor of classes as factories.
So, I'm going to visually represent a class as this factory like object.
So, you can imagine needs smokestacks and this is the factory.
So, when we have an init method,
we're saying that we have one method inside of the class called underscore init.
This is a function like object that lives within the class.
So, it accepts three arguments it accepts self and it accepts arguments for x and y.
So, this method in underscore in it,
the constructor lives inside of the class.
Then when we have get x,
this method also lives inside of the class.
So, we have this method inside of the class get x.
In this case it takes in one argument which is self and then we have the method get y.
Again get why lives inside of this class.
Now, on line 10 when we create a new instance of point what this is doing,
is it's asking the class to create a new instance.
I'm going to represent this instance like so.
In every instance has a set of instance variables,
in this case the instance variables are x,
which we set to 10 and y,
which we set to 100.
Now, I want to go into a little more detail as to how exactly we
set these instance variables x and y to 10 and100.
In other words, what exactly happens when we say point 10,100?
So, I'm going to erase this instance for now.
Let's start from scratch.
So, when Python evaluates this statement point 10,100,
then what it does is it first creates a new kind of blank slate instance.
So, it creates a new instance of
point and it doesn't have any instance variables at first.
The next thing that Python does is it asks,
does this point class have a constructor or in other words this underscore init method?
In the case of point there is an underscore init method.
So, what Python does is it calls that underscore init method on this empty instance.
So, what it's going to do is it's going to take this instance and pass it
in as the value for self when we call the constructor.
In all of this is done before we even have finished evaluating this statement.
So, we take this instance in and that's the value of
self and then in this case we're also passing in two additional arguments.
We're passing in 10 for x and 100 for y.
When we're passing in values for x and y these are just function arguments.
So, in other words even though x just so happens
to be the name of what's going to later on be an instance variable.
There's nothing special about the value x.
So, now we're calling the constructor underscore init and we're
passing in our new instance which is blank and we're passing in again values for x and y.
Now, on line three when we say self dot x equals x.
Then what that does is it sets the instance variable for this instance.
So, it's setting the instance variable x to whatever this value x that we passed in it.
So, in this case we passed in 10 for x.
So, we're setting the instance variable for this instance x to 10. Same thing for y.
In this case we're setting y to the value 100 because that was what was passed in.
So, now we have this instance in it has two instance variables x and y.
That's all for now, until next time.