Transcript

Use with ChatGPT / Claude Auto-scroll A

1. Chapter 1 (0:00)

0:00 - 0:19
The following content is provided under a Creative Commons license.Your support will help MIT OpenCourseware continue to offer high-quality educational resources for free.To make a donation or view additional materials from hundreds of MIT courses, visit MIT OpenCourseware at ocw.mit.edu.
All right, everyone.Let's get started.So, um so, today is this going to be the section second lecture on object-oriented programming.So, just a quick recap of last time.Um on Monday we saw um we were introduced to this idea of object-oriented programming and we saw these things called abstract data types.And these abstract data types we implemented through uh Python classes.And they allowed us to create our own data types that sort of abstracted a general um object of our choosing.Right?So, we've used lists before, for example, but with abstract data types we were able to create objects that were of our own types.We saw the coordinate example and then at the end of the class we saw the fraction example.So, today we're going to talk a little bit more about object-oriented programming and classes.We're going to see a few more examples.
And um we're going to talk about a few other um uh nuances of classes, talk about information information hiding and class variables.And in the second half of the lecture we're going to talk about uh the idea of inheritance.So, we're going to use object-oriented programming to sort of simulate how to simulate um how real life works.So, in real life you have inheritance and in uh in object-oriented programming you can also simulate that.Okay, so the first few slides are going to be a little bit of recap just to make sure that everyone's on the same same page before I introduce a couple of new concepts related to classes.So, recall that when uh in the last lecture, we we talked about writing code from two different perspectives, right?The first was from someone who wanted to implement a class.So, implementing the class meant defining your own uh object type.
So, you defined the object type when you define the class, and then you decided what data attributes you wanted to define in your object.So, what what data makes up the object?What is the object?Okay?In addition to data attributes, we also saw uh these things called methods.And methods were ways to tell someone how to use your data type.So, what are ways that that someone can interact with the data type?Okay?So, that's from the point of view of someone who wants to write their own object type.So, you're implementing you're implementing a class.And the other perspective was to write code from the point of view of someone who who wanted to use a class that was already written.Okay?So, this involved creating instances of objects.So, you're using the object type.Once you created instances of objects, you're able to do operations on them.
So, you're able to see what methods whoever implemented the class added, and then you can use those methods in order to uh do operations with your um instances.So, just looking at the coordinate example we saw last time a little bit more in detail about what that meant.So, we had a class definition of an object type, which included deciding what the class name what the class name was.And the class name basically told Python what type of an object this was.Okay?In this case, we decided we wanted to name a coordinate we wanted to create a coordinate object, and the type of this object was therefore going to be a coordinate.We define the class in the sort of general way, okay?So, we needed a way to be able to access data attributes of any instance.So, we use this self variable, okay?
And the self variable was we we used to refer to any instance um to the data attributes of any instance in a sort of general way without actually having a particular instance in mind, okay?So, whenever we access data attributes, we would say something like self.To access a data attribute, you'd access the attribute directly with self.x.Or if you wanted to access a method, you'd say self.and then the method name, for example, distance.
And really the bottom line of of the class definition is that your class defines all of the data, so data attributes, and all of the methods that are going to be common across all of the instances.So, any instance that you create of a particular um object type, that instance is going to have this exact same structure, okay?The difference is that every instance's values are going to be different.So, when you're creating instances of classes, you can create more than one instance of the same class.So, we can create a coordinate object here using um this syntax right here.So, you say the type and then whatever value it takes in.And you can create more than one coordinate object.
Each coordinate object is going to have different data attributes.Uh sorry, it's going to have different data attribute values, okay?Every coordinate object is going to have an x value and a y value.But the x and y values among different instances are going to are going to vary, okay?So, that was that's the difference between defining a class and looking at a particular instance of class.So, instances have the structure of the class.So, for a coordinate, an all instances have an X value and a Y value.But, the actual values are going to vary between the different instances.Okay.So, ultimately, why do we want to use object-oriented programming?So, so far the examples that we've seen are um were were numerical, right?A coordinate, a fraction.But, object-oriented using object-oriented programming, you can um create objects that mimic real life.So, if I wanted to create objects of uh an object that defined a cat and an object that defined a rabbit, I could do that with object-oriented programming.
I would just have to decide as a programmer what data and what methods I'd want to assign to um to these to these groups of objects.Okay?So, using object-oriented programming, each one of these is considered a different object.And as a different object, I can decide that a cat is going to have a name, an age, and maybe a color associated with it.And these three here on the right, each one of these rabbits is also an object and I'm going to decide that I'm going to represent a rabbit by just an age and an and a color.Okay?And with object-oriented programming, I can using these um using these attributes, I can sort of groupthese three objects together and these three objects together.Okay?
So, I'm grouping sets of objects that are going to have the same attributes together.And attributes, this is also a recap of last time, come in two forms, right?Data attributes and procedural attributes.So, the data attributes are basically things like um are basically things that define what the object is.So, how do you represent uh cat as an object?And it's up to you as a programmer to decide how you want to do that.For coordinate, it was pretty um pretty straightforward.You had an X and a Y value.If we're representing something more abstract like an animal, then maybe I would say, "Well, I'm going to represent an animal by an age and a name."Okay?So, it's really up to you to decide how you want to represent what data attributes you want to have uh uh uh to to have to represent your object with.

2. Chapter 2 (8:24)

Procedural attributes were also known as methods.And the methods are essentially asking, "What can your object do?"Okay?So, how can someone who wants to use your object, how can someone interact with it?So, for coordinate, we saw that we could find the distance between two coordinates.Maybe for my abstract animal object, you might make it uh you might have it make a sound.Okay?By maybe printing to the screen or something like that.Okay.This slide's also a recap of how to create a class.Just to make sure everyone's on the same page before we go on.So, we defined a class using this class keyword, and we said class, the name of the class.So, now we're going to create an a more abstract animal class.We're going to see in the second half of the lecture what it means to put something else in the parentheses, but for now, we say that an animal inher- an animal is an object in Python.
So, that means it's going to be it's going to have all of the properties that any other object in Python has.And as we're creating this animal, we're going to define how to create an instance of this class.So, we say d e f, and this init was the special method that told Python how to create an object.Inside the parentheses, remember we have the self, which is a variable that we used to refer to any instance of the class.Okay, we don't have a particular instance in mind, we just want to be able to refer to any instance.Okay, so we use the self variable.And then in here and then the second parameter here is going to represent um what other data we use to initialize our object with.In this case, I'm going to say I'm going to initialize an animal object with an age.
Okay, so when I create an animal, I need to give it an age.Inside the init are any initializations that I want to make.So, the first thing is I'm going to assign an instance variable age.So, this is going to be the data attribute age to be whatever is passed in.And then I'm also making another assignment here where I'm assigning the data attribute name to be none originally.Later on in the code, when I want to create an animal object, I say the class name and then I pass it in whatever parameters it takes, in this case the age.And I'm assigning it to this instance here.Okay?All right.So, now we have this class animal.We've done the first part here, which is to initialize the class, right?So, we've told Python how to create an object of this type.There's a few other methods here that I've implemented.
Next two, we call getters and the two after that we call setters.Okay?And getters and setters are are very commonly used when um when it when uh when implementing a class.So, getters essentially return the the of any of the data attributes.Okay?So, if you look carefully, get age is just returning self.age.And get name just returns self.name.So, they're very simple methods.Similarly, set age and set name, we're going to see what this what this funny equal sign is doing here in the next couple of slides.But, setters do a very similar thing where they're going to set the data attributes to whatever is passed in.Okay?
So, those are getters and setters.And then, the last thing down here is this str method.And this str method is used to tell Python um how to print an object of this type animal.So, if you didn't have this str method, if you remember from last lecture, what ends up happening is you're going to get some message when you print your object that says, "This is an object of type animal atthis memory location."which is very uninformative, right?So, you implement this method here which tells Python how to print uh an object of this type.Okay?So, the big point of this slide is that you should be using getters and setters.You should be implementing getters and setters for your classes.And we're going to see in the next couple of slides why exactly.But, basically, they're going to prevent bugs from uh from coming into play later on if someone decides to change the implementation.
So, we saw how to So, So, the previous slide, this slide here, shows the implementation of the animal class.And here we can see how we can create an instance of this object.So, we can say A is equal to animal three.So, this is going to create a new animal object with an age of three.And we can access the object through uh the variable A.Dot notation, recall, is a way for you to access data attributes and methods of a class.Okay?So, you can say a.age later on in your program and that is allowed.It'll try to access the age data attribute of this particular instance of the class A.So, this is going to give you three.However, it's actually not recommended to access data attributes directly.So, this is the reason so, you're going to see in the next slide the reason why we're going to use getters and setters.
Instead, you should use the get age getter method to get the age of um of the animal.So, this is going to return also three.So, these are going to do the same thing.
And the reason why you'd want to use getters and setters is um this idea of information hiding.Okay, so the whole reason why we're using classes in object-oriented programming is so that um you can abstract certain uh certain data from the user.Okay, one of the things you you should be abstracting is these uh these data attributes.So, users shouldn't really need to know how a class is implemented.They just They should just know how to use the class.Okay?So, consider the following case.Let's say um whoever wrote the animal class wants to change the implementation.And they've decided they don't want to call the data attribute age anymore.They want to call it years.Okay, so when they initialize an animal, they say self.years is equal to age.So, an animal still gets initialized by its age.And the age gets passed into a data attribute named years.
Okay?Since I'm implementing this class, I want to have a getter which is going to return self.years.So, I'm not returning self.age anymore because age is no longer uh the data attribute I'm using.So, with this new implementation, if someone was using uh if someone was using this implementation and was accessing age directly as what as accessing the data attribute age directly.With this new implementation, they'd actually get an error, right?Because this animal that they created using my old implementation no longer has an attribute named age.And so Python's going to spit out an error saying no attribute found or something like that.Okay?If they were using the getter a.get_age, the person who implemented the class re-implemented get_age to work correctly, right?With their new uh data attribute.Um so uh years as opposed to age.So, if I was using the getter uh get_age, I wouldn't have run into the bug.
Okay?So, things to remember, use getters and write getters and setters for your classes, and later on in your code, use getters and setters to prevent bugs and um and uh and to promote easy to maintain code.Okay.So, information hiding is great, but having said that, Python is actually not very great at information hiding.Okay?Python allows you to do certain things that you should never be doing.Okay.So, the first we've just seen.So, it's to The first is to access data attributes from outside of the class.Okay?So, if I were to say a.age, Python allows me to do that without using a getter and setter.Python also allows you to write to data attributes from outside the class.So, if I implemented the class animal assuming that age was a number, an integer, and all of my methods work as long as age is an integer, but someone decided to be smart and outside of the class set

3. Chapter 3 (17:09)

age to be infinite as a string, that might cause the the the to crash.Okay?Python allows you to do that, but now you're breaking the fact that age has to be an integer, right?So, now the methods should probably be checking the fact that age is an integer all the time.The other thing that you're allowed to do is to create data attributes outside of the class definition.Okay?So, if I wanted to create a new data attribute called size for this particular instance, Python also allows me to do that.And I can set it to whatever I want.So, Python allows you to do all these things, but it's actually not good style to do any of them.Um so, just don't do it.All right.So, the last thing I want to mention, the last thing about classes before we go on to inheritance is this idea called default arguments.
And default arguments are passed into methods, and since methods are functions, you can also pass in default arguments to functions.So, for example, this set name method had self and then this new name is equal to this empty string here.Okay?We've haven't seen this before.But, this is called a default argument.And you can use the function in one of two ways.
The first way is so we can create a new instance of an animal type object with this line here, A is equal to animal three.And then we can say A.set_name.So, this calls the setter method to set the name.And notice we've always said that you have to put in parameters for everything other than self.Okay?But, here we have no parameters passed in.But, that's okay because new name actually has a default argument.Okay?So, that tells Python if no parameters passed in for this particular formal parameter, then use whatever is up here by default.So, if I haven't passed in the parameter a.getname a.setname, sorry, a.setname is going to be setting the name to the empty string because that's what the default parameter is.So, in the next line, when I print a.getname, this is just going to print the empty string.
Okay?If you do want to pass in a parameter, you can do so as as as normal.So, you can say is equal to animal three, a.setname, and then pass in a parameter here.And then new name is going to be assigned to whatever parameter passed in.Like that.Whatever you pass in overrides the default argument, and everything is is good.So, when I print a.getname, this is going to print out the name that you that you've passed in.
Questions about default?Yeah.

4. Chapter 4 (20:16)

What if you don't provide a default value for for new name?If you don't provide a default argument for new name, and you do this case here, then that's going to give you an error.So, Python's going to say something like expected one argument, got zero, or something like that.Great question.
Okay.All right.So, let's move on to this idea of hierarchies.Okay?So, the great thing about object-oriented programming is that it allows us to add layers of abstraction to our code.Right?So, we don't need to know how very, very low-level um things are implemented in order to use them, and we can build up level we can build up our code to be more and more complex as we useup these different um these different abstractions.So, consider every one of these things on the slide as being a separate object.Right?Every one of these things is can be considered to be an animal.Okay?
According to our implementation of an animal, the one thing that an animal has is an age.Okay?And that's probably true, right?Every one of these things has an age.But now I want to build up on this and create separate groups, right?And each one of these separate groups that I that create on top of animal is going to have its own functionality, right?They're going to be a little bit more specific, a little more specialized.So, I can create these three groups now, a cat, a rabbit, and a person group.And for example, so they're all animals, right?They all have an age.But for example, maybe a person's going to have a list of friends.Whereas a cat and a rabbit do not.Maybe a cat has uh a data attribute for the number of lives they have left, right?Whereas a person and a rabbit do not.
Okay?So, you can think of adding these more specialized adding more adding functionality to each one of these subgroups.Okay?So, they're going to be more and more specialized, but all of them retaining the fact that they are animals.So, they all have an age, for example.So, on top of these, we can add another layer and say that a student is a person and is an animal.Okay?But in addition to having an age and maybe also having a list of friends, a student might also have a major.Or they're pretty young, so maybe uh their favorite, you know, their favorite subject in school.
Um So, so that's the general idea of of hierarchies.Okay?So, we can sort of abstract the previous uh the previous slide into this one and say that we have parent classes and child classes.Okay, the animal class is like our parent class.It's the highest level class.

5. Chapter 5 (23:13)

Inheriting from the animal class, we have these child classes or subclasses.Okay?Whatever an animal can do, a person can do.Whatever an animal can do, a cat can do.And whatever an animal can do, a rabbit can do.Okay?That is have an age and maybe some really basic functionality.But between person, cat, and rabbit, they're going to be varying widely as to the kinds of things that they can do, right?But they can all do whatever animal can do.So, child classes inherit all of the data attributes and all of the methods or behaviors that their parents classes have.Okay?But child classes can add more information.Like, for example, a person can have a list of friends, whereas a general animal will not.
It can add more behavior.Like, maybe a cat can climb trees, whereas people and rabbits cannot.Or you can also override behavior.So, in the previous one, we had animal, person, student.So, maybe we have an animal doesn't speak at all, but a person can speak.So, that's added functionality to the person.And maybe a person can only say hello, but then when we talk to a student, we can override the fact override the speak method of a person and say that a student can say, you know, I have homeworkor I need sleep or something like that.Okay?So, we have the same speak method for both person and student because they can both speak, but student will override the fact that they say hello with something else.Okay.So, let's look at some code to put this into perspective.So, we have this animal class which we've seen before.
This is the parent class.Okay?It inherits from object, which means that everything that a basic object can do in Python and an animal can do, which is things like binding variables, you know, very low-level things, okay?We've seen the init, we've seen the two getters, the setters, and the string method to uh to print an object of type animal.All right, now let's create a subclass of animal.We'll call it cat.Okay?We create a class named cat.In parentheses, instead of putting object, we now put animal.And this tells Python that cat is going cat's parent class is animal, so everything that an animal can do a cat can do.So, that includes all of the attributes, which was age and name, and all of the methods.So, all the getters, the setters, the STR, the init, everything that the animal had, now the cat has, the cat class has.
In the cat class, we're going to add two different two more methods, though.Okay, the first is speak.So, where So, so speak is going to be um a method that's going to just take in the the self, okay?No other parameters.And all it's doing is printing me out to the screen.Very simple, okay?So, this through the speak, we've added new functionality to the to the class.So, an animal couldn't speak, whereas a cat says meow.Additionally, through this STR method here, we're overriding the animal STR, okay?So, if we go back to the previous slide, we can see that an animal uh the animal's STR had animal plus the name plus the age here.
Whereas the cat's STR now says cat, name, and the age, okay?So, this is just how I chose to implement this.Okay?So, here I've overridden the STR method of of the animal class.Notice that this class doesn't have an init, and that's okay.Because Python's actually going to say, "Well, if there's no init in this particular method Sorry, in this particular um class, then look to my parents and say, "Do my parents have an init?"Right?And if so, use that init.So, that's actually true for any other methods.
So, the idea here is when you have hierarchies, you have a parent class, you have a a a child class, you can have a child class to that child class, and so on and so on.So, you can have multiple levels of inheritance.What happens when you create an object that is of type something that's that's been of a type that's the the the child class of a child class of a child class, right?What happens when you call a method on that object?Well, Python's going to say, "Does the Does a method with that name exist in my current class definition?"And if so, use that.But if not, then look to my parents.Do my parents know how to do that?Right?Do my parents have a method um for whatever I want to do?If so, use that.If not, look to their parents, and so on and so on.
So, it's you're sort of tracing back up your ancestry to figure out if you can do this this method or not.So, let's look at a slightly more complicated example.We have a class named person.It's going to inherit from animal.
Inside this person, I'm going to create my own I'm going to create an init method.And the init method is going to do something different than what the animal's init method is doing.It's going to take in self as usual, and it's going to take in two parameters as opposed to one.A name and an age.First thing the init method's doing is it's calling the animal's init method.Why am I doing that?Well, I could theoretically initialize the the name and the age data attributes that animal initializes in this method.But, I'm using the fact that I've already written code that initializes those two data attributes.So, why not just use it?Okay?So, here this says, "I'm going to call the class animal.I'm going to call its init method, and I'm going to leave it up to you to not you as the class, but I'm talking to as as as the program is running, I'm going to leave it up to
you to figure out how to initialize an animal with with this particular age and what to name it."So, Python says, "Yep, I know how to do this, so I'm going to go ahead and do that for you."So, now it says person is an animal, and I've initialized the age and the name for you.The next thing I'm doing in the init is I'm going to set the name to whatever name was passed in.Okay?So, in the init notice I can do whatever I want, including calling function calling methods.And then the last thing I'm doing here is I'm going to create a new data attribute for person, which is a list of friends.Okay?So, an animal didn't have a list of friends, but a person is going to.The next four methods here are the this one's a getter, so it's going to return the list of friends.
This is going to append a friend to the end of my list.I'm making I I want to make a note that I actually didn't write a method to remove friends.So, once you get a friend, they're friends for life.But, that's okay.Um the next method here is speak, which is going to print hello to the screen.And the last method here is going to get the age difference between two people, so that just basically subtracts their age and says it's a 5-year age difference or whatever it is.And down here I have an STR method, which I've overridden from the animal which instead of animal colon name it's going to say person colon name colon age.Okay?

6. Chapter 6 (30:52)

So we can run this code.So that's down here.I have an animal
person here.
So I'm going to run this code.And what did I do?I created a new person.I gave it a name and an age.I created another person a name and an age and here I've just run some methods on it which was get name, get age, get name, get age for each of the two people.So that printed Jack is 30, Jill is 25.If I print P1 this is going to use the STR method of person so it's going to print person colon their name and then their age.P1.speak just says hello and then the age difference between P1 and P2 is just five.So that's just subtracting and then telling uh printing that out to the screen.

7. Chapter 7 (31:49)

Okay, so that's my person.Let's add another class.This class is going to be a student and it's going to be a subclass of person.Since it's a subclass of person it's going to a student is going to inherit all the attributes of a person and therefore all the attributes of an animal.The init method of a of a student is going to be a little different from the one of person.We're going to give it a name, an age, and a major.Notice we're we're using default arguments here.So if I create a student without giving it a major the major is going to be set to none originally.Once again this line here person.init self name age tells Python hey you not argue know how to initialize a person for me with this name and this age, so can you just do that?"
And Python says, "Yes, I can do that for you."And so that saves you maybe like five lines of code just by calling the init method that you've already written through person.Okay?So student has been initialized to be a person, and additionally we're going to set another data attribute for the student to be the major, and we're going to set the major to be none.The student is going to get the setter here, the setter method, which is going to change the major to whatever else they want if they want to change it.And then I'm going to override the speak method.So the speak method for the person, recall, just said hello.A student is going to be a little bit more complex.I'm going to use the fact that someone created this random class.Okay?So this is where we can write more interesting code by using by reusing code that other people have written.
So someone wrote a random class that can do cool things with random numbers.So if I want to use random numbers in my code, I'm going to put this import random at the top of my code, which essentially brings in all of the methods from the random class.One of the methods being this random method.So random open close parentheses is a random method from the random class.And this essentially gives between zero and one, including zero but not including one.Okay?So this random number I get here is going to help me write my method for speak, where it's going to with quarter 25% probability, it's either going to say I have homework, I need sleep, I should eat, orI'm watching TV.Okay?So student is going to say one of those four things.And the last thing I'm doing down here is overriding the STR method.
So let's look at the code.I'm going to comment this part out.And uncomment the student and see what we get.
Okay, so here I'm creating the student.I'm creating one student whose major is CS, name is Alice, and age is 20.S2 is going to be another student named Beth, age 18, and the major is going to be none because I didn't pass in any major here.So, by default, using the default argument, it's going to be none.If I print S1, S2, that's going to print out these two things over here.Just by whatever STR method does.And then I'm I'm going to get the students to speak.And if I run it multiple times, you can see that it's going to print different things each time.So, I need sleep.I have homework.I need sleep.I have homework.Yeah.So, every time it's going to print something different.

8. Chapter 8 (35:37)

Okay.Questions about inheritance?This example.
Okay.Last thing we're going to talk about in this class is an idea of uh or in this lecture is the idea of a class variable.Okay?So, to illustrate this, I'm going to create yet another subclass of my animal, called called a rabbit.
So, class variables, so so far we've seen Sorry, let me back up.So, so far we've seen instance variables, right?So, things like self.name, self.age.Those are all instance variables.So, they're variables that are specific There are common across all of the instances of the class, right?Every instance of the class has this particular variable.But, the value of the variable is going to be different between all of the different instances.So class variables are going to be variables whose value is whose values are shared between all of the instances in the class.So if if one instance of the class modifies this class variable, then any other instance of the class is going to see the modified value.So it's sort of shared among all of the different instances.So we're going to use class variables to keep track of rabbits.Okay.
So we're creating this class rabbit.Tag is equal to one.We haven't seen something like this before.So tag is our class variable.Class variables are typically defined inside the class definition, but right but outside of the init.So tag is going to be a class variable and I'm initializing it to one.Inside the init, this tells us how to create a rabbit object.So I'm going to give it self as usual, an age, and then two parents.Don't worry about the two parents for now.Inside the init, sorry, inside the init, I'm going to call the init of the animal because just to do less work.Python already knows how to initialize an animal for me, so let's do that.So that's going to set the two data attributes name and age.I'm going to set the data attributes for parent one, parent two for rabbit to be whatever's passed in.
And then this is where I'm going to use this class variable.So I'm creating this data attribute instance variable particular to a specific instance called RID.Okay?And I'm assigning this this um instance variable to the class variable.And I access class variables using not self, but the class name.So, in this case, rabbit.tag.
So, initially, tag is going to be one, and then the init is going to increment the tag by one here.Okay?So, that means that from now on, if I create any other instances, the other instances are going to be accessing the updated value of tag instead of being one.So, let's do a quick drawing to show you what I mean.So, let's say I have um rabbit.tag here.Okay?So, initially, tag is going to be one.Okay?And then, I'm going to create a new rabbit object.So, this is as I'm calling the code.Okay?So, let's say this is a rabbit object.Oh, boy.Okay.R1.You know, I actually Googled how to draw a rabbit, but that didn't help at all.Okay.So, R1 is going to be a new rabbit that we create.Initially, what happens is when I first create this new rabbit, it's going to access the class variable, which its current value is one.
So, when I create the rabbit ID, the rabbit ID, R1.RID,this is going to get the value one.And according to the code, after I set the rabbit ID to whatever tag is, I'm going to increment the tag.So, this is going to say, "Okay, now that I've set it, I'm going to go back up here and increment the tag to be two."Okay.So, let's say I create another rabbit object.Okay?All right.There.That's a sad rabbit.R2.The ID of R2 is going to be what?Well, according to the way we the way we create a new rabbit object is it's going to access whatever value of tag is, which is a class variable.It was changed by the previous creation of my rabbit, so now I'm going to access that, right?So, the value is going to be two.
And according to the code, the next thing I do after I create after I create the instance RIDs, I'm going to increment tag.So, I'm incrementing the class variable to be three.Okay.So, notice that all of my instances are accessing this shared sort of the shared sort of resource.The shared variable called tag.So, as I'm creating more and more rabbits, they're all going to be incrementing the value of tag because it's shared among all of the instances.And so this so this value this this tag class variable sort of keeps track of how many different instances of a rab of how many different instances of rabbits I've created throughout my entire program.Okay.So, the big idea here is that class variables are shared across all the instances, so they can all modify them.But these RIDs, right?These instance variables are only um are only for that particular instance.
So, R2 can have access to RIDs R1's ID value, nor could change it, but it won't change it across all of the different instances.Okay.
So, so that'sthat's how the init method works of rabbit.Okay?So, we have these tags that keep track of how many rabbits we've created.We have a couple of get We have some getters here uh to get all the parents.So, now let's add a somewhat more interesting function.Oh, I just want to mention when I'm getting the RID, I'm actually using this cool zfill function here or method, which actually pads the beginning of of any number with however many zeros in order to get to thatnumber here.So, the number one becomes 001 and so on.So, it ensures that I have uh this nice-looking sort of ID type thing that's always three digits long.So, let's try to work with this rabbit object.Let's define what happens when you add two rabbits together.Okay?In this class, not in in the real world.

9. Chapter 9 (42:53)

Okay.So, if I want to use the plus operator between two rabbit instances, I have to implement this __add__ method.Okay?So, all I'm doing here is I'm returning a new rabbit object.Okay?Whoops, sorry about that.And let's recall the init method of the rabbit.Okay?So, when I'm returning a new rabbit object, I'm returning a new rabbit object that's going to have an age of zero.Self, so the rabbit object I'm calling this method on is going to be the parent of the new of the new rabbit, and other is going to be the other parent of the new rabbit.Okay?So, if we look at the code,
and I run it,
this part here, I'm creating three rabbits, R1, R2, and R3.Notice this class variable is is working as expected because the IDs of each of my rabbits increment as I create more rabbits.So, we have 001, 002, 003.If I print R1 and R2 and R3, that was these three lines over here.The parents of R1 and R2 are none because that's just the default um the default argu- the yes, the default arguments for creating a rabbit.To add two rabbits together, I use the plus operator between two rabbit objects.And on the right here, I'm testing rabbit addition.And I can print out the uh uh the IDs of all my rabbits.And notice that when I've created this new rabbit R4, the ID of it still kept incrementing.So, now the ID of the fourth rabbit is double double 04.And then when I get R4's parents, they are as as we want them to be.
So, R1 and R2.

10. Chapter 10 (45:05)

The other thing I want to do is to compare two rabbits.So, if I want to compare two rabbits, I want to make sure that their parents are the same.So, I can compare the parent uh the first parent of the first rabbit with the first parent of the second rabbit, and the first parent of the first second parent of the first rabbit to the first second parentof second rabbit, or getting the combinations of those two.So, that's what these are these two booleans are doing.So, these are going to tell me these are going to be boolean values, either true or false.And I'm going to return either we they have the same parents of that type or the same parents crisscrossed.Okay?So, here notice that I'm actually comparing the IDs of the rabbits, as opposed to the rabbit objects directly.Okay?
So, if instead of doing if instead of comparing the IDs in here, I was comparing the parents themselves directly, what would end up happening is this function this method EQ would get called over and over again Because herewe have parents that are rabbits.And at some point the parents of the very very first rabbits ever created by this program are none.And so when I try to call uh when I try to call uh the parent one of none, that's going to give me an error, okay?Something like an attribute error where it doesn't none doesn't have a this parent attribute.Okay?So that's why I'm comparing IDs here, okay?And the code in the lecture here shows you some tests about whether um rabbits have the same parents, and I've created new rabbits here, R3 and R4.The addition of those two.And R5 and R6 are going to have the same parents down here, true.
But R4 and R6 don't.Okay?So just to wrap it up, object-oriented programming is the idea of correcting creating your own collections of data where you can organize the information in a very consistent manner.So every single type of object that you create of of this particular type that you create Sorry.Every object object instance of a particular type is going to have the exact same data attributes and the exact same methods, okay?So this really comes back to the idea of decomposition and abstraction in programming.All right.Thanks, everyone.