Transcripts
1. Object Oriented Programming Introduction: Welcome to the complete object oriented programming
in Python masterclass. If you're learning Python
and wanted to take your programming skills
to the next level. And this class is for you. You don't need any prior
knowledge to get started. So what is object oriented
programming used for? You might ask, OOP is used in computer science to
implement data structures. It's also used in
web development, databases, data science, artificial intelligence,
game development, graphical user interfaces,
and so much more. In this class, you will learn
all the fundamentals of object oriented programming in Python and apply them
to build applications. So let's get started. Shall we?
2. Classes and Objects: Hello and welcome
back to my video. Today we'll be taking a look at object oriented programming
in Python, or in other words, o p. With object
oriented programming, you can make your own
custom classes to suit your needs and use those
classes through objects. So what really is an object? You will be surprised
that you might be using objects on the daily
without knowing it. So here's an example of
objects we are using on the daily if we print
the type of an integer, for example, this is
what we're going to get. As you can see, it
says class integer. So this number that we typed
over here is an object of the class and
similar to a string. The word math here is an
object of the class string. Similarly, when we
create a function, we're really just
creating an object of that original class called
function for example. So now we established
that most things we use in Python are objects
of their own class. So let's learn how to create our own classes with their
own functionalities. So let's create a class
and its simplest form. It is always recommended
to capitalize the first letter
in our class name, which in this case is cat. If we want to use this class, we have to create an
object of this class. So c1 is the name of the object and it's
calling the class Cat. And let's sit on it and
see how it looks like. This cat class almost does
nothing besides printing. I am a cat, which is
really not that useful. We expect our class
to take names, ages of the users do
stuff, et cetera. This is why the init
method comes in play. All classes have a
method called init, which is always executed when the class is
being initiated. We're going to use the net
method to assign attributes or other operations
that are necessary to do when the object
is being created. For example, if I wanted to
create a class of a person, I wanted to take values
such as name and age. So we define a
class named person. Now we define the net method. As you can see, it's
such a default in classes that python automatically
writes the whole thing. For me, the self argument
is a reference to the current instance
of the class and is used to access
class variables. Now, let's add
arguments to our class. Now, let's use our class
by creating an object. As you can see when I hover
over the parentheses, it says parameter, age
and name are not filled. So let's fill them respectively. Remember, it is very
necessary to write self dot before assigning our
variables name and edge. Now let's add another
method to our class, the method that prints out
details about the person. Now that we have defined P1, P2, and P3 to be an
object of our class. They can use its
method, get details. So what happens if we
create a forth before without assigning that
required variables like name and edge, we will get an error saying it's missing the two
required arguments. So you might be asking, why is the init
method so special? Why do we have to define it by default every time
we create a class? Because by nature, when
we type P equals person, we're expecting to fill
out the details there right away and then use
any method on the class. For example, if we
commented out and it, which basically makes it now
invisible to our program, as you can see those now to
turn an unexpected argument. So now we have to create an entire method that takes
inputs from the user. So for example, now we'll have to remove
the arguments and insert them through
insert details method. Now we can use the
get details method. So not only this
is confusing for the user on everyone who's
dealing with this code. It's also time-consuming. And if anything, classes
were supposed to do was to shorten the time
needed to do things. So is the net method necessary? Not really, the class work
just fine without it, but it sure helps a lot. So this is the code
without the net method, and this is the code
would then add method. So to recap, we create
the class called Person and gave it the following
attributes, name and age. We defined methods in our class to give it
some functionalities, such as get details. And then we created objects of this class called V1, V2, V3, and we filled out the
necessary parameters and we use the get
details method. So let's try another example. Say for instance, we created
a dog and a cat class, or do they take name and age? So let's remove those. Obviously, we can create an
object from these classes. If you notice both classes
are almost identical, with the only
difference being that they print out
different strings. So we had to write it twice when we could've
wrote one parent general class that includes the genetic information about
each animal, for example. But how do we link
these classes? You might ask, how do you allow cat and dog classes to
inherit attributes from the more general class
animal simply by opening parentheses and typing the balance class
we're going to add, in this case animal. Now both cat and
dog classes have inherited the init method
from the parent class animal, and we no longer need to
specify it each time. So let's go ahead
and remove them. Now. Let's use our methods and make sure they are working fine. So not only we made our code shorter while still
retaining the same features, but we can now add methods
that apply to both cat and dog classes without
having to add them to cat and dog specifically,
for example. Now let's use this
method, cat for example. Oh, I forgot to add a t here. Even though we didn't define this method in the cat class, cat still inherits those methods from its parent class animal. So that's it for the basics. The next video we'll be taking a closer look about inheritance and
different types of it. So that's it for today's video. Thank you so much for watching and I hope to see
you on the next one.
3. Inheritance: Hello and welcome
back to my video. Today we'll be taking
a look at types of inheritance and object-oriented
programming in Python. So why is inheritance
useful and biofilm, it accurately depicts
real-world relationships. It allows for code reusability. We don't have to rewrite the same code over
and over again. It allows us to add new features to our class without
having to alter it. It is transitive in nature, which implies that if class B inherits from
another class a, then all of these subclasses will automatically
inherit from class a. With that in mind,
let's take a look at different types
of inheritance, starting with
single inheritance. So say for example, I wanted to create
a country class that would represent Canada. I would have a parent
class called Canada. Now we will create
a child class that inherits from Canada
called Ottawa. Since Ottawa is a city
and a part of Canada, any features that get added to Canada should automatically
be applied to auto. Now I will create an
object of the child class Ottawa and use its method city. Since Auto as a child
class of Canada, I can also use its method country name without having to define
it inside of O2. So for example, the second
type is multiple inheritance. And multiple inheritance,
the features of all the base classes are inherited into the
derived class. So let's create two parents
classes, mother and father. Now we will create
a child cluster inherit from these two classes. Now, let's create an object from class sun and use features
from its parent class. As you can see, we
were able to use father name and mother name, even though they weren't
defined in our class sun. And that's because we
inherited those names from the two classes,
mother and father. The third type is
multilevel inheritance. Inherits both base class and derived class features to
the newly derived class. So let's see an example. Notice in both child classes we invoke the constructor
of the parent class. Now, let's create objects of our classes and start using it. So the reason we pass
three parameters is because the sun's
class takes three names. As you can see, the
code works just fine. So what happens if we removed invoking the constructor
of Grandfather class? As you can see, the code breaks. The fourth type is
hierarchical inheritance. It's a situation in
which the balanced class is inherited by
multiple subclasses. Now we have a barons glass
with two child classes. Remember, those two
derived classes can use the functions and features
from their parents class. However, child one cannot use
functions from channel two. So far so good. They can use the
parent's class features and their own features. However, the moment
we tried to use a function from child one and child two, we will get an error. And finally, the fifth
type, hybrid inheritance. In this type features of
more than one time of inheritance are mixed to
form hybrid inheritance. So let's see an example. Now let's create
objects of our classes. Finally, let's see how we could put all of this
together and create a calculator program using object oriented
programming functions, f statements, and while loops. First, let's create a
calculator class to include all our
mathematical operations. Now we will need to
create an object for our class Calculator so
we can start using it. Now let's create a while
loop and set it to true and prompt the user to enter their desired
mathematical operation. The advantage of using the
while loop here is that this program will keep running unless the user chooses five, which in this case we'll
have to make an F statements saying F choice
equals equals five, then break, which
will break the loop. Anyways, let's continue
with our program and create a logic for it
using f statements. This will make sure the user
has entered a valid choice. First, let's check whether the
user wants to exit or not. If not, then let's
ask for the input from the user to go ahead
with the calculations. Now let's create an F LF
statement to match their choice. Let's start with add our program and see
how it looks like. Then we press F5,
it should exit. But our program is
still not perfect. If the user entered
the string will get a value error because wife and couldn't convert a
string to an integer. So let's put our
exception handling skills to use and
deal with this adult. As you can see, this works
by handling our error, but it doesn't rerun
the program for us. For that, we can simply
copy paste the code under the except block so that when it runs it also execute our code. Now of course, there are
flaws to this method. For example, if the user to enter a string
another time you will get an error
because we expect it to handle the cell only once. This is where retry
comes in play, but we don't want to
divert the video too much. So we just set up with this. In conclusion, you don't
really need to be limited by a certain and handwritten
style as much as you should focus on
keeping your code structured. Meaning you should follow
your own structure that suits your own needs. So that's it for today's video. Thank you so much for watching and I hope to see
you on the next one.
4. Static Methods, Class Methods, Class Attributes: Hello and welcome
back to my video. Today we'll be taking a
look at static methods, class methods, and attributes, starting with class attributes. So what is class attributes? A class attribute is a
variable that belongs to a class rather than
a particular object. So let's take an example. So now we have created a
simple animal class will then init method in which you
pass the name of the animal. So let's create an
instance of that class. You might be asking, what is this variable over
here, number of animals? Well, this is a class attribute. And right away what makes
it a class attribute is that it doesn't
use self like name. So what's the difference
between this class, attribute, number of animals and
something like name, for example, one name changes
from instance to instance. For example, A1 will have
a different name than A2. However, both A1
and A2 will have the same number of
animals, num of animals. It's not dependent
on an instance. So for example, So much so that I could
derive the animal, that animals, and I would
still get the same results. Meaning I can access this
class attribute from the class itself without having to create an instance like
this one over here. So now that we have
established that number of animals is a class attribute that applies for
the whole class. What can we do with it? First, we can keep track of how many instances of our class animal has
been created order, in other words, how
many animals we have. So for example, notice
we used animal not-self, otherwise, it would've
gave us different results. So that's the basic idea
behind class attributes. There are some users
to them, of course, such as being able to
define a constant that you want to use for any
instance in that class. So for example, you
had a physics class with Lightspeed obviously
being a constant. Instead of defining
it outside of your class as a global variable. You can instead
define it inside of your class as a class attribute so you can
access it easier. So why is this a
better approach? We always want to make sure your classes as
independent as possible. Meaning if you were to use this class somewhere else
at some point in time, it has all of its
methods and variables predefined inside of it and ready to be used somewhere else. So that's how you can use class attributes to
your advantage by creating any variable that you might need inside of the class, instead of creating it
outside as a global variable. Now let's talk about
class methods. So remember how we incremented
num of animals over here. Every time an instance of the
class animal was created, we can do that a bit
definitely using class method. So let's create a class method
and see how it looks like. As you can see, we use
TLS instead of self. Now we're going to return
the number of animals. What we also need
to do is to use a class decorator to denote that this is indeed
a class method. So now we have created a class method that returns
the number of animals. We need a class
method that adds plus one once an animal or
an instance is created. So let's create
another class method. So how can we use the Add
animals class method? For example, we can remove
animal that number of animals from our init method and replace it by ad animals. Now what this will
do is it will call the class method add
animals every time. And it was called, meaning
every time an instance of the class animal
has created like A1, A2, it will use the class
method and animals. So let's see if
this works by using num animals class method,
it should return to. So that was class methods. Let's take a look
at static methods. Here are some of the
characteristics of static methods. First, it's bound to the class. It cannot modify a
class or object state, and it cannot access or modify the class and its
instance variables. So sometimes when we're
creating a calculator program, we tend to define methods like you could define those methods inside of a class and have them as static methods. So for example, now we can use the
class decorator to denote that this is
a static method. To use those methods, instead of creating an instance
of the class calculator, we could just access them
right away using calculator. So for those methods, I don't have to create an instance of the class
calculator like this. And then use C1. Oh, sorry. I could just use calculator right away without having
to create an instance. I agree that some
might say, Well, you could have defined those
methods globally and used ad right away instead of
calculator, like for example. There are some
complications to it. And it's mainly
to keep your code more structured and organized. So that's it for today's video. Thank you so much for watching and I hope to see
you on the next one.
5. Importing Classes: Hello and welcome
back to my video. Today we'll be taking a
look at different methods. You can import classes
until program, starting with the most
straightforward method in which the file or class you're importing is within your workspace or directory. So here we have two Python
files, men and test. They're both in the
same directory. So we have a class, a test that pie that we
want to use at main.py. So as you can see, test.py
has a class called calculator and we
want to import that. And to buy, to do that,
we'll do the following. First, we will import
the name of the file. In this case, it's test. So let's go ahead
and abort test. Then to use the class, just like any other class, you'll have to
create an instance of an object of that class. So let's create an
instance of test. As you can see when I type dot, it shows me that there's
a class called calculator that I should choose in
order to start using it. So let's choose calculator. Now, just like any instance, we can go ahead and use its
methods and attributes. As you can see, it
works just fine. Notice I had to type the name
of the file which is test, and then choose a certain class within that file,
which has calculator. That means I could have
multiple classes within the test file that I could
choose from. For example. Now let's use the physics class. So that's assuming
the class we are importing is within
our directory. What happens if I want
to import a file on class at my desktop,
for example. To do that, we'll
first import success. Then we will type CIS dot path. Then we will type the location of the file we will import. We can know the
location of the phi by heading over to
the file itself. Right-click, then
click on Properties. Now we'll import
the file itself, which is test file. Now to start using it just like any class we need to
create an instance of it. So I imported the class
math and use the method add to know what class and
methods we have in our file. Simply we can open
it using Notepad. So what we did is important. The final test file then created an instance of the class
math through test file. Then we use the method
add inside of that class. So that's it for today's video. Thank you so much for watching and I hope to
see you in the next one.