OOP is about representing real world objects as computerized models called classes.
A class is a template or type and an object is an instance of that class/type.
When you make a class, you are making a type like bool, int, etc, but you name it something else.
You can give a class any atrribute you desire. Like a health value, a dmg value, a name value, all of these are attributes in the class which are made of different data types.
Instantiation
Making an object from a class is called instantiation. Do you see it? INSTANCE. INSTANTIATON! You make an instance from a class.
Python class creation and instantiation
We are going to use python to do OOP.
this script is to create dog classes.
We are making a datatype DOG.
and then we have a init method for constructing the class.
so, when you instantiate, you will give it 2 arguments. The name argument and the age argument.
And we are going to make those atrributes relative to the dog.
Dog will have a name and an age.
Ok, then we have a sit method. This will just print out something very simply cause we are too bored to actually make a dog.
this is a method that just needs to be called. No arguments needed to be fed into it. We print our dog name and we say that it is sitting down.
Afterwards we instantiate into a variable.
and then we print the type out just to prove that we have made our own datatype.
and lastly, we will run our method we just made.
so the final output is this:
there is also, and this is sidetracking a bit, but there is also a .title() function in python that just captializes the first letter in a string. So, abbey → Abbey. Just do .title() after a string.
Functions vs methods:
Functions and methods are both very similar and can be used interchangably for the most part, but a method is usually used when talking about object-oriented programming. However, since a method is associated with an object, a method run will implicitly take the self argument as the object it was used on.
Remember, all methods have a self parameter for the first one. This one is implicitly called everytime for with different objects being the ‘self’.
The class is the definition, an object is just an instance of that data.
https://stackoverflow.com/questions/155609/whats-the-difference-between-a-method-and-a-function
right, so I also wanted to experiment with default parameters since I remember it is possible.
yes, I tried it out, and this works. You can give default values to arguments if they were not given beforehand.
init method:
The init method is called automatically when instantiation takes place. It is the constructor. The reason why there are double underscores on each side is to prevent python’s default method names from clashing with your own method names. Again, this is a method so you will always pass self as the first parameter of this method.
its also advised to use the constructor to lay out the attributes of this object. Make all your traits inside the constructor.
Attributes
An attribute is a variable special to each object
you just do self.attributename = value
very simple. And you can modify this value later on in your class methods or for current instance manipulation.
Callable classes
For some reason, a class is also callable. So if you instantiate a class and then just call it like a function, you can actually return or modify values.
Don’t know why anybody would use this, but it exists.
To do this, just do call as a method in the function.
and then I just call the object directly
’
Class inheritance
So, if you did some experimenting, you may realize that we can make a class without the parenthesis.
both of these do the exact same thing, only due to the fact that there is nothing inside the parenthesis of the second declarator.
Now, if there was something inside those parenthesis, we would be able to inherit.
So, assume we have 2 classes here. One is for the class dog, the other is for the class cat.
now, what you will notice is that cat and dog are pretty much the same, except for the fact that the speak method prints out different strings. They are almost identical. Now, the idea behind inheritance is that if you have several similar classes, instead of writing the same class twice, you can acutally inherit from an upper-level class like boilerplate. What we can inherit is the init method here, because that is the method that is the one that is being repeated.
So, how we make an upperlevel class is this:
so this is our upper level class.
They will also inherit the show method from this Pet class as well.
Lets try that out.
an inheritance is simply just adding brackets after the class with the inherit parent after it
for real!
Now, an important thing to note is that if you redefine a method in a lower-level child class, that method will override whatever method is in the upper-level parent class.
see above that the cat class is redefining the speak method. And now, then we instantiate and then run the speak method for both pet and cat instances these are the results:
you can also just make the most useless pet in the world that is 100% boilerplate
just put a pass after the declarator
now, say that we want to change our init method to have new attributes.
the above code cannot slide. We cannot let this slide. Not on my watch, hell no.
we have 2 options:
-
manually do self.name = name; self.age = age
-
do a super().init(name, age)
what super does is it references the super class which in our case is the Pet class. init finds the method we want to call. And then name, age. It will run the lines in the initialization that assign values to name and age.