Welcome back, everyone, to part three of our continued discussion, object oriented programming.
We're going to discuss methods.
Let's head back to our code editor.
All right.
So here I am inside Visual Studio Code Editor Now, when we're talking about methods, they're really
just functions defined inside the body of a class, and they are used to perform operations with the
attributes of our objects.
Now let's go ahead and create a class called Circle.
So we have our instance of a circle class.
And then for most classes, we're going to be creating an instantiation method or in its call.
So double underscores, remember, and this is going to take in self and what are the attributes associated
with a circle?
Well, one of the attributes can be the radius, so we can have a user define the radius of a circle.
So I can say radius and then pass in, oops, forgot the colon.
There we go.
Say self.
The radius is equal to the radius passed in by the user.
Now, if you want a default value for your radius, you can always say something like radius equal to
one, and that will set a default value to one against the actual user doesn't define radius upon creating
a circle.
So now outside of this, I can say my circle is equal to an instance of circle, and if I wanted to,
I could say something like radius.
Equal to 20, for example.
And then I could print out my circle and print out its specific radius, so it's going to run this just
to check if it works.
And now I see the output of 20, so I have this very simple circle class and then in order to create
a circle, I do need to define its radius.
Otherwise, we'll have a default radius of one.
If we think back to class object attributes things that are true for any instance of the class, such
as Circle, I could say that PI is a class object attribute, so I could say PI is equal to three point
one for save that change.
And so three point four makes sense to be a class object attribute because regardless of the radius
of the circle, it should always have the same PI attribute.
Three point one For now, let's talk about adding in more methods.
This init method is technically a method within the actual circle class, but now we want to expand
on things that are circle class can do.
For example, let's imagine I wanted to calculate the area of a circle.
I could create a method for that, so I could say something like the F area.
And then to make it clear that I'm going to be performing this method using attributes from the instance
of the class I pass in self here that essentially connects this function to be a method operating on
the class.
And here I can decide whether I want to print or return.
Typically, methods are going to return in the same way functions usually a return they don't usually
print.
So to calculate the area of a circle, it's going to be its radius squared times PI.
So I'm going to say return self-taught radius.
Notice how I say self dot radius because I'm passing in self, which now allows me to have access to
self, but radius up here.
I can't just say radius by itself because I was only passed in in the in its method.
It's not actually being passed tense area, which is why I say self, but radius.
Now remember it's self that radius squared.
So I could say times self radius and it's pi are square.
So I do need to multiply it by PI.
There's two ways I could do this because PI is a class object attribute.
I actually have the ability to say circle that PI and I can do this circle that pi.
There we go.
I can do this for class object attributes because PI doesn't change regardless of the instance of the
circle or what I can also do is, say, self-taught pi either is OK.
Circle that PI basically shows somebody who's reading that method that, by the way, PI is a class
object attribute, but either of these methods should work just fine.
So no, again what this area method does, it takes its self and then it can use attributes in order
to return something.
So I'm going to define a circle with a radius of four.
And then I'm going to call area and notice how I'm not actually passing in open and close parentheses
into area.
We'll see why in just a second.
In fact, we should be calling parentheses, but I want to show you what happens when you don't.
If I just run it like this, it says, Hey, this is a bound method, circled the area of this particular
circle object at this point in your computer's memory, this area.
Notice how it's not actually inside in it.
In fact, area, just like a function, needs to be executed with open close parentheses.
If you just say my circle dot area, that's not actually executing this function slash method.
Instead, what it's doing is just reporting back to you.
Hey, area is this bound method for a circle object created at this point in memory to actually execute?
Area, I need to open close parentheses on it.
Now, when I run this, I see that the area of the circle of Radius four is equal to fifty point two
four.
Let's go ahead and try to finding another method.
Let's make this method for perimeter.
In fact, try to pause the video and see if you can do this yourself.
So how do we define perimeter?
Well, we're going to say is create another method called perimeter.
Again, it takes itself that way.
I can access those attributes.
And then I'm going to return the perimeter, which is just two times the radius, times PI.
So I should be returning to.
Times the radius, self-taught radius.
Times softball pie, or if it's a class object attribute, you could also say circle that pie.
It's up to you when you want to use.
So let's now instead of calling area called the perimeter.
Save that change.
Go ahead and run this now I can see the perimeter being reported back and again, this can be circled
that pie, or it could also be soft that pie.
Now you may be wondering which one's better to use circled up higher self the pie.
Some people sometimes they prefer to use circle because it lets someone know as they're reading this
method.
Oh, by the way, circle that pie.
That's a class object attribute not defined by the user.
Other people like the consistency of just having everything self.
Either way, it's going to work the same, so I wouldn't worry about it too much for our use cases.
In fact, we're not going to see too many class object attributes as we continue to work with our own
object oriented programming creation within the context of Django.
All right.
So let's again review our methods.
We have this init method, which we have for most of our instances for classes and then we can define
our own methods.
These are really simple methods that I did all on one line.
But keep in mind, I could create new variables here, so I could say something like, Hey, inside
of this area.
Go ahead and do something like results is equal to self thought radius squared, and then maybe I want
to update results within results as equal to results times self-taught pi.
And then here return.
Result after it got updated, so this is the same thing, it's pi times our squared and here I'm just
returning the result.
But just keep in mind, I'm now able to use these variables that I want within the context of this method.
Note that I don't need to say self here because this is a little temporary variable that only lives
within the scope of the area method when we're talking about things like self.
Those are attributes of the object that can be accessed as long as we say self inside the actual method
of call, which by convention we typically always do.
All right.
So those are the way method works.
You can think of them as just functions that you're defining inside of the class that can then access
attributes of the class in order to perform some sort of calculation.
And keep in mind, you can also make method calls that update the actual attributes themselves.
It's not as common as grabbing the attribute, but you could go ahead and do something like that.
So I'm going to create kind of a silly method here, and this silly method is going to be called double
radius.
And then takes in self.
And what this methods actually going to do is what it says.
It's going to take the existing radius and then double it.
So it's going to say self that radius is equal to so that radius times to note what's happening here.
I'm saying grab the current radius, multiply it by two and then reset the existing radius to that.
Keep in mind, this method doesn't really make sense to return anything, I could maybe print out a
result saying something like radius has been.
Doubled to.
And maybe use f string literal formatting here, that may be something I want to see reported back up
to user and then I would simply say self-taught radius here.
This is just to give you the idea that the methods themselves can permanently change the attribute values.
So let's go ahead and check this out.
I'll create a circle for radius of four and then instead of calling perimeter, I'm going to say.
Double the radius and here, I don't need to actually print anything up because nothing's being returned.
So I'm calling my circle double radius, which should I expect to grab the current radius and reset
it to the double of it?
Then it's going to report back that the radius has been doubled, although keep in mind, it doesn't
actually return anything, it's just printing out the report.
This is just to show you that methods can effectively update within attributes, and they can also take
in arguments and then do whatever they want to the current existing object.
So I go ahead and run this and it says, Hey, radius has been doubled to eight.
In fact, let's go ahead and prove it to ourselves by saying print.
My circle, that radius, which I should also see, is now eight after I had initially defined that.
As for?
And there it is, Radius has been doubled to eight.
And so when they print out that radius, it has been indeed updated to be eight methods.
It should also be pointed out, can take their own parameters.
So now this is going to be called multiply.
Radius, and now I'm going to take in a parameter to multiply by, so some numbers multiply.
And so instead of saying self-built radius times two, I'll say.
Self-taught radius times the number, and then I'll say radius has been changed to the updated now self-taught
radius.
So instead of calling double radius here, I'll say multiply radius.
Now keep in mind, multiply radius as visual studio codes telling us requires some number to be passed
in.
Let's see what happens.
If I don't pass in that number, I should expect an error.
And there's the error.
It says, Hey, multiply radius is missing that require two positional argument number, so I can pass
that in.
Let's say 20.
I run this an hour radius has been changed to 80, and I can see the uptick here four times 20 is 80.
That's just to give you the idea that methods can accept their own arguments and then use those arguments
inside.
You should also note how this particular argument not being within in it did not require something like
soft, but no, because I'm only using this number at the scope of multiplied radius.
I'm going to permanently affect the attribute radius, but I don't need to bother, say this particular
number as some sort of attribute of the circle object because it's just a number that the user happened
to pass in.
OK, that's it for simple methods.
Coming up next, we'll talk about inheritance.