Welcome back. So, we've already seen that
every instance of a class can have its own instance variables.
So for example, here we have a point class and inside of the constructor _init_,
we can see that every point instance has instance variables for
X and Y to represent the position of that particular point.
Now, for example, here when we create a new instance of point with X equal 2, Y equal 3,
then suppose that our point class is represented with this notation again,
so this represents our point class and p1 is an instance of
the point class that has instance variable X set
to 2 and instance variable Y set to 3.
Now, we also have another instance of our point class,
so that's p2 and p2
has X set to 3 and Y set to 12.
All right, so we have p1 and we have p2.
So, one thing that I want to note is that,
instances have their own variables but classes can have their own class variables.
They work just like instance variables except that they belong to the class itself.
So for example, here the point class has its own class variable called printed_rep.
So I'm going to write that inside of the class point,
so I'll say printed_rep is set to a star.
Now, the point class also has these methods which are also a form of class variables,
so we have to _init_ and its value is
some function and we
have this graph method whose value is a different function.
Now, whenever python searches for anything
of the form instancename.instancevar,
so for example, if we're doing p1.graph or p2.graph or
even something like print p1.printed_rep or print p1.x,
then the search order that the python evaluator goes in is,
it first searches inside of the instance.
So in this case,
if we're searching for p1.x and then it searches inside a p1
and it finds x there and it evaluates that to be 2.
If it doesn't find an instance variable with the name
x or in this case let's go with the example of p1.printed_rep,
so if we're searching for p1.printed_rep,
python first searches inside of the instance p1,
it doesn't find it there,
so then it searches inside of the point class and that's where it finds it.
When we say p2.graph,
python first searches inside of p2,
it doesn't find it there and so it searches inside of the point class where it finds it.
If we had something like print p1.z,
then python would first search inside of p1,
it wouldn't find z there.
It wouldn't find z there.
It would then search inside the point class,
it wouldn't find z there either and so it would give us a run-time error.
Now, the way that we define this point class,
I erase these annotations,
the way that we define the point class is that we have
this graph method and what graph does is a kind
of forms a text graph or a textual representation of a graph that contains this point.
So, if I erase my annotations and I run my code,
so here I create two instances of point,
one at 2, 3 one at 3,
12 and I call p1.graph and what that does is it
creates a little textural representation where here you can see kind of an X axis,
here you can see kind of a Y axis and I can see that my point p1 is at 2.
So 2 and 3 and I can see that my point p2 is at 3 and 12.
Inside of this representation of graph,
I refer to self.printed_rep.
So you'll note that here,
printed_rep again is a class variable that's set to a star.
If I change printed_rep to be let's say a lowercase x,
then you'll see that printed_rep changes for
both p1 and p2 when we actually called the graph method on both of them.
So, that's an example of how classes can contain variables.
So again, we have instance variables that belong to
every particular instance and we have class variables which belong to a class.
Here technically, there are three class variables,
there's printed_rep, _init_ and graph that all belong to the point class.
That's all for now until next time.