Okay, let's take a look at a real life Object Oriented Programming
example. So up until this point, we've learned about classes,
class attributes, class methods, and sort of the 'self' keyword,
and all sorts of stuff behind the scenes there.
But we actually haven't used any real example.
So let's use an example where, let's say maybe we have a service
where we are working with people's pets.
And so we have different types of animals here.
So we can say 'class Animal'.
And in here we can use '__init__', the magic method
'__init__', always takes 'self'.
And what 'type' of animal is it?
What is the animal's 'name'?
And instead of 'type', because that is actually a word, let's
use 'species'. Let's ask for a 'name'.
And I don't know, let's give it a 'weight'.
And so we can say 'self.species
= species', 'self.name = name',
and 'self.weight = weight'.
Then because every animal can speak, it's going to say something, so
'def speak(self)',
and because I don't know what this animal is, it's just going
to print a grunting sound.
I guess. Most animals know how to grunt, right?
And so let's save that.
And let's create a new animal here.
So I have an animal.
His name is "zephyr".
He's going to be a new 'Animal()' class.
He is a house "Cat", his name is "Zephyr", and his weight is
"14.5 lbs".
He is a larger cat.
I also have another cat.
His name is Henry animal cat.
His name is "Henry", and he's a little bit smaller.
He is "9 lbs".
So what we've done here is, we've written a template for an
'Animal' that's always going to have a 'species', a 'name', and a 'weight'.
And without having to rewrite 'species', 'name', and 'weight'
every single time, we wrote out this template, and then we
instantiated a new 'Animal' class, and we specified
the 'species', 'name' and 'weight'.
And the logic that happens behind the scenes is already done for us.
So at any point in time, I can say, 'zephyr.speak()',
I can also say 'henry.speak()',
[no audio]
and so with just one line of code for each one of these,
I have now effectively used nine lines of code behind the
scenes. Now, if you're still not convinced on why Object
Oriented Programming is useful, without it we would have to
do one of two methods.
The first method is really gross.
It uses a lot of variables.
So 'zephyr_name = "Zephyr"'.
'zephyr_species = "Cat"'.
'zephyr_weight = "14.5 lbs"'.
Okay, not bad, not bad,
but now I also need to specify "henry".
So, 'henry_name = "Henry"'.
'henry_species =',
like you get the point here, I'd have to write this out for
every single animal.
Now, the thing is down the road is, what if I wanted to get
Zephyr's weight?
But let's say this was accidentally not set.
How do I get his weight?
Well, I have to check to see if that variable even exists,
and then if that variable does exist, it is very specifically
just Zephyr.
It's not Henry, it's not anyone else.
This variable is literally just for Zephyr.
So the reusability of this variable is a lot lower than if
we used it in a class.
[no audio]
The second option is, we can say 'zephyr' is an object or not an
object, but a dictionary.
And then we could say 'name': "Zephyr",
'species': "Cat".
'weight': "14.5 lbs".
Now, this is actually not a terrible way of doing things.
However, if I wanted to also do the same thing about "Henry",
I would have to copy this over and change his name to "Henry",
which, again, not a terrible thing, but copy and paste can
be dangerous because we tend to forget things.
So he's "9 lbs".
We can set that as well.
So that's a little bit easier.
But let's say you have Zephyr, Henry, Ted, Ezra, and maybe
like a pet lizard or something.
And you have to do this for every single pet.
Well, what if down the road you were changing 'weight' from
pounds to kilograms, you would have to find every single
instance of every single animal, which means you have to
manually scan your code because there's only one reference
to 'zephyr' here,
there's only one reference to 'henry' here, well, there's two,
but in a variable name, there's only the one reference.
Or if you use the class, you could add some logic in here.
And you could say 'if weight.endswith("lbs"): "#Convert to
kilograms". And then you could say 'self.weight =
whatever the weight is going to be in kilos.
And now you've effectively updated all of your code.
So even if someone puts in "14.5 lbs", it's going to say
it ends with "lbs",
and it's going to convert that weight to kilograms.
And not only do you only have to do it in the one place,
you no longer need to do a search and find for 'zephyr', 'henry',
'ted', 'ezra', your pet lizard.
You don't have to do it for every single one.
And in the variable example, you don't have to go ahead and
change this from "14.5 lbs" to "14.5 kilos" or kilograms, and
do the math behind it, because your program will do that for you.
That is a very, very good reason and a good real life example
of why we would want to use Object Oriented Programming.
It is a nice template for repeatable code.
[no audio]