Coding

Everything you need to know about Classes in Python! (Object Oriented Programming Tutorial)

  • 00:00:00 hey what's up guys and girls and welcome
  • 00:00:01 back to another video today we are going
  • 00:00:04 to be talking about glasses sorry
  • 00:00:06 classes no classes there we go
  • 00:00:11 classes and more generally
  • 00:00:13 object-oriented programming is
  • 00:00:15 fundamental to becoming any sort of
  • 00:00:16 software engineer whether you want to be
  • 00:00:18 a data scientist a mobile app developer
  • 00:00:21 a back-end engineer you're gonna need to
  • 00:00:23 know about classes in this video we will
  • 00:00:26 start with the basics what our class is
  • 00:00:28 how can we define them in Python when
  • 00:00:31 and why should we use them and as we go
  • 00:00:33 throughout the video we'll build up some
  • 00:00:34 more advanced concepts you know we'll
  • 00:00:36 talk about sub classing we'll talk about
  • 00:00:38 operator overloading and I'm gonna try
  • 00:00:41 to make it very visual so you can really
  • 00:00:43 drill down these concepts quickly and
  • 00:00:45 efficiently before I begin I want to
  • 00:00:47 give a quick shout out to this video
  • 00:00:49 sponsor and that is kite kite is a
  • 00:00:51 machine learning based code completion
  • 00:00:53 tool for Python that works with all of
  • 00:00:55 the most popular editors like atom vs
  • 00:00:58 code sublime vim and pycharm kite up to
  • 00:01:03 code faster by giving you all sorts of
  • 00:01:05 code completion suggestions sorted by
  • 00:01:07 relevance and it can give you
  • 00:01:09 completions that are up to full lines of
  • 00:01:11 code which is really cool if you're
  • 00:01:13 interested in checking out kite I left a
  • 00:01:15 link in the description I have been
  • 00:01:17 playing around with kite for about a
  • 00:01:18 month now and I've had a very very good
  • 00:01:20 experience it's super fun and it has
  • 00:01:22 made me code faster one of the best
  • 00:01:24 parts I forgot to mention is it is
  • 00:01:25 completely free to download so
  • 00:01:27 definitely check out that link in the
  • 00:01:28 description but without further ado
  • 00:01:30 let's get into this video to start off I
  • 00:01:34 want to give a little bit of background
  • 00:01:35 on why we should use classes so when I
  • 00:01:37 was at MIT we had a model that if you
  • 00:01:38 wanted to write good code it should be
  • 00:01:40 safe from bugs easy to understand and
  • 00:01:42 ready for change in classes and
  • 00:01:44 object-oriented programming really hit
  • 00:01:46 on these three points they make our code
  • 00:01:48 safe from bugs by allowing us to kind of
  • 00:01:50 isolate the different parts of our
  • 00:01:51 system we can easily write test cases
  • 00:01:53 for one specific class while abstracting
  • 00:01:56 away the details of the other
  • 00:01:57 dependencies they make our code easy to
  • 00:02:00 understand by providing structure to our
  • 00:02:03 code we can group all of the variables
  • 00:02:05 and methods that kind of share an object
  • 00:02:08 and functionality together and separate
  • 00:02:10 objects from one another and then they
  • 00:02:12 make our code ready for
  • 00:02:13 we can easily add new class methods to
  • 00:02:15 existing objects and if we ever need to
  • 00:02:18 provide completely new functionality we
  • 00:02:19 can always make a new object while kind
  • 00:02:21 of not screwing up anything else we've
  • 00:02:23 already written I can only teach you so
  • 00:02:25 much though by telling you all of this
  • 00:02:26 let's start writing some code so you can
  • 00:02:28 really get a hang of it yourself I'll be
  • 00:02:31 using Python 3 and sublime text as my
  • 00:02:33 editor and let's start off by writing
  • 00:02:35 our first class and I want this visit
  • 00:02:38 this tutorial to be very visual so I'm
  • 00:02:40 gonna be using shapes as kind of my
  • 00:02:42 example to start off so let's define a
  • 00:02:44 class called polygon and so the
  • 00:02:46 convention to do this is class and then
  • 00:02:49 usually you define your class with a
  • 00:02:50 capital letter so we have this polygon
  • 00:02:54 class so polygons triangles squares
  • 00:02:57 Pentagon's hexagons etc that's what
  • 00:03:00 we're gonna be defining with this class
  • 00:03:01 so to start off when we define a polygon
  • 00:03:05 we define it with a certain number of
  • 00:03:08 sides and a name or something so when we
  • 00:03:11 initialize stuff in a class we always
  • 00:03:13 use this convention and it might seem a
  • 00:03:14 little bit weird at first but you'll see
  • 00:03:16 throughout this tutorial why it's useful
  • 00:03:19 so we do def in it and then the first
  • 00:03:22 parameter we always pass in to our knit
  • 00:03:25 method is self and then we need to think
  • 00:03:29 about what's important to a polygon well
  • 00:03:31 maybe the number of sides is important
  • 00:03:34 whether you know you have four sides in
  • 00:03:35 our square or if you five sides in our
  • 00:03:37 Pentagon and maybe we want to give it a
  • 00:03:39 name too so we define our polygon
  • 00:03:41 Hutton's by the number of sides and the
  • 00:03:44 name and the last step whenever
  • 00:03:46 initializing things and this will not
  • 00:03:48 make sense at first but as I go through
  • 00:03:50 it will make sense we do self dot sides
  • 00:03:53 equals sides and self dot name equals
  • 00:03:58 name okay with that we've defined our
  • 00:04:02 first class let's see a couple examples
  • 00:04:04 of us using this class in action so I'm
  • 00:04:07 going to start off by defining a square
  • 00:04:09 which is equal to a polygon and based on
  • 00:04:12 what we set in this knit we will have to
  • 00:04:16 pass in sides a name so to define a
  • 00:04:18 polygon we'll have to pass in the sides
  • 00:04:20 and so let's define a square that has
  • 00:04:23 four sides and then we'll give it the
  • 00:04:25 name
  • 00:04:26 and let's also define a Pentagon so
  • 00:04:29 Pentagon is a polygon with five sides
  • 00:04:33 and the name Pentagon cool cool
  • 00:04:40 and now what we can do is I can do print
  • 00:04:46 square
  • 00:04:47 dot sides and as you can see it says
  • 00:04:52 four down here I can do print square dot
  • 00:04:56 name and it says square so we've
  • 00:05:01 basically what's happened is by feeding
  • 00:05:03 in these parameters 4 and square
  • 00:05:06 we have passed in we have created a
  • 00:05:09 polygon class and when we pass these in
  • 00:05:14 we set the sides and we set the name
  • 00:05:17 through this in it method and as you can
  • 00:05:20 see with Pentagon
  • 00:05:28 pentagons both sides and then we can do
  • 00:05:30 a print Pentagon dot name the same thing
  • 00:05:36 is true for the Pentagon
  • 00:05:37 so we've captured we've kind of
  • 00:05:38 encapsulated the information through
  • 00:05:41 this polygon class of a square and a
  • 00:05:43 Pentagon and to quickly clarify the only
  • 00:05:45 things we need to pass into our method
  • 00:05:47 so when we're initializing the polygon
  • 00:05:49 is the stuff after this this the stuff
  • 00:05:52 after self the sides a name I'm gonna
  • 00:05:56 get to self in a little bit in this
  • 00:05:57 video but just keep in mind that when
  • 00:06:00 you're creating these objects you just
  • 00:06:02 have to worry about the stuff after self
  • 00:06:04 now it's cool to be able to see all this
  • 00:06:05 information about the classes we've
  • 00:06:07 initialized but let's move on and do
  • 00:06:09 something actually cooler and that's
  • 00:06:10 actually draw the shapes based on the
  • 00:06:12 parameters we pass into the class so to
  • 00:06:15 do that we can define a class method so
  • 00:06:19 I'm gonna define a class method called
  • 00:06:20 draw and normally wanted to find a
  • 00:06:22 method if I don't need to pass in any
  • 00:06:24 variables we can just do it like this
  • 00:06:26 draw and you know and you know into the
  • 00:06:29 functionality here as we need to but
  • 00:06:31 because this is a class method the first
  • 00:06:34 thing that we're gonna want to do is
  • 00:06:35 pass in self to this method and we're
  • 00:06:40 gonna slowly start building up what self
  • 00:06:41 is but basically we have these
  • 00:06:44 properties in our class sides and name
  • 00:06:46 we pass itself to our class method to
  • 00:06:49 allow us to access these parameters
  • 00:06:51 without having to like mainly type in
  • 00:06:54 sides name every time you want to use
  • 00:06:57 this function so we can just access them
  • 00:06:59 by passing in self and this will make
  • 00:07:01 sense in a sec and also we need a way to
  • 00:07:03 actually draw the polygon so to do that
  • 00:07:05 let's import the turtle library it's a
  • 00:07:08 nice easy visual library for Python and
  • 00:07:12 something cool I mentioned I don't know
  • 00:07:13 if you just saw the right side of my
  • 00:07:15 screen but I'm currently using kite
  • 00:07:17 copilot and something that's really cool
  • 00:07:19 about it is that it tracks my cursor
  • 00:07:21 location on the screen and can tell me
  • 00:07:24 all sorts of documentation about where
  • 00:07:27 I'm at in the code which is very very
  • 00:07:29 nice and if you know my videos I'm
  • 00:07:32 always dragging in Google searches for
  • 00:07:34 documentation so this is a really useful
  • 00:07:37 feature for me to kind of like know that
  • 00:07:40 mutation as I write the code okay so
  • 00:07:42 we're gonna import the turtle library
  • 00:07:43 and we want to draw our shape so the way
  • 00:07:47 we can think about it is we need a for
  • 00:07:49 loop for each side in our shape and
  • 00:07:52 let's think of these as regular polygons
  • 00:07:54 so they each have the same size so to
  • 00:07:56 find this draw method we'll can do
  • 00:07:59 something like for I in range self dot
  • 00:08:02 sides so we're getting whatever number
  • 00:08:04 we passed in here and then we're going
  • 00:08:06 to want to go turtle dot forward we'll
  • 00:08:08 say let's say maybe a hundred hundred
  • 00:08:12 pixels and then turtle dot hmm now we
  • 00:08:16 need to turn a specific angle so if
  • 00:08:19 we're doing a square we would need to do
  • 00:08:21 like turtle dot right ninety degrees and
  • 00:08:25 as you can see we have to pass in the
  • 00:08:26 angle to this function turtle dot right
  • 00:08:30 90 degrees this would allow us to do a
  • 00:08:32 square but we need to be able to do this
  • 00:08:34 for any sheep we pass in so we'll have
  • 00:08:36 to probably edit our own it method a bit
  • 00:08:37 to give us an angle to well let's just
  • 00:08:39 start with seeing what a square would
  • 00:08:41 look like and I'm gonna just end this
  • 00:08:43 method with a turtle dot done I might
  • 00:08:45 end up putting this somewhere else
  • 00:08:47 but for now we'll leave it here okay so
  • 00:08:50 now let's go ahead and do square dot
  • 00:08:52 draw and see what happens
  • 00:08:55 look at that we just drew a square using
  • 00:08:58 the turtle library that's pretty cool
  • 00:09:00 okay so we knew we had to turn right 90
  • 00:09:03 degrees for a square but let's abstract
  • 00:09:06 this away and make it more general
  • 00:09:08 generalizable so we can do any shape
  • 00:09:10 using this right method so if you dig
  • 00:09:13 back into your high school geometry days
  • 00:09:15 you might know the formula for interior
  • 00:09:17 angles of polygons but if you don't
  • 00:09:19 here's a helpful little chart we can use
  • 00:09:21 to kind of get the details on what our
  • 00:09:24 values are going to be so the sum of
  • 00:09:27 interior angles of a triangle is 180 the
  • 00:09:30 sum of a square is 360 540 etc and the
  • 00:09:34 general formula for any polygon is n
  • 00:09:37 minus 2 where n is the number of sides
  • 00:09:40 times 180 degrees and then if we want a
  • 00:09:43 specific angle and that's what we do one
  • 00:09:45 in our case it's n minus 2 times 180
  • 00:09:48 divided by the number of sides because
  • 00:09:50 if you have a total of 360 degrees
  • 00:09:53 four sides each angle has to be 90
  • 00:09:56 degrees and that's the interior angles
  • 00:09:58 here so let's start writing some logic
  • 00:10:00 into our class that captures that so we
  • 00:10:04 could define a poly Oh polygon interior
  • 00:10:07 angles property that is equal to well
  • 00:10:13 it's going to be equal to the number of
  • 00:10:15 sides that was the end part minus two
  • 00:10:18 and we're going to put this in
  • 00:10:21 parentheses times 180 right that was the
  • 00:10:28 interior angle sum so you can maybe
  • 00:10:33 define this like sum of angles and tiri
  • 00:10:36 angles whatever you want so that would
  • 00:10:37 be the all the angles if we wanted just
  • 00:10:40 the individual angle it's going to be
  • 00:10:42 equal to self dot interior angles
  • 00:10:46 divided by self dot sides and now we
  • 00:10:52 have to find two more properties of our
  • 00:10:54 class and these ones are computed on
  • 00:10:57 initialisation so it doesn't matter it
  • 00:11:00 depends on what number of sides we pass
  • 00:11:02 in so let's see that now in action if I
  • 00:11:06 do go back to what we were printing out
  • 00:11:09 before and I did print square dot let's
  • 00:11:14 say interior angles and I did print
  • 00:11:17 square dot angle this should give me 360
  • 00:11:23 and this should give me 90 let's see if
  • 00:11:27 it does look at that 360 and 90 cool
  • 00:11:31 cool okay and now what we can do is pass
  • 00:11:35 in that angle here so we can do self dot
  • 00:11:38 angle because we can use the self method
  • 00:11:41 or the self parameter that we passed
  • 00:11:43 into our class method to access the self
  • 00:11:45 dongle that we initialized on the
  • 00:11:48 creation of our class to turn right 90
  • 00:11:51 degrees okay cool so let's see if now
  • 00:11:55 with passing in this we get the same
  • 00:11:57 result of that draw method so we're
  • 00:11:59 going to do square draw and notice I
  • 00:12:02 don't have to pass in self here which
  • 00:12:04 might seem a little bit strange
  • 00:12:06 self is the one thing that's kind of
  • 00:12:09 just known to be there so whenever you
  • 00:12:12 see self you kind of think about you
  • 00:12:13 don't have to include anything there you
  • 00:12:15 just skip it it's like it's not there
  • 00:12:17 when you're actually trying to define to
  • 00:12:20 utilize a method you just use what it's
  • 00:12:23 after yourself so like when we
  • 00:12:24 initialize the polygons we initialize
  • 00:12:27 the stuff after yourself and when we use
  • 00:12:29 draw even though their self in it we
  • 00:12:31 don't have to pass anything in it that's
  • 00:12:33 a little weird at first but it should
  • 00:12:34 make sense over time so let's do square
  • 00:12:36 draw look we get that same square all
  • 00:12:40 right real test will be though does it
  • 00:12:42 work with Pentagon draw come on draw
  • 00:12:46 Pentagon
  • 00:12:47 ah no why did it not work so when we're
  • 00:12:51 doing turtle dot right we're defining
  • 00:12:53 this exterior angle so this right now is
  • 00:12:55 what we defined by self dot angle but we
  • 00:12:59 really want it to be this interior angle
  • 00:13:01 so do compensate for that we're gonna
  • 00:13:04 just need to take the complement so if
  • 00:13:06 this interior angle here should be 108
  • 00:13:09 we need to do 108 or 180 minus 108 on
  • 00:13:13 the outside so we can actually define
  • 00:13:17 when we do the right method we can do
  • 00:13:19 180 minus self dot angle and that should
  • 00:13:22 be give us the right result look at that
  • 00:13:25 got it cool all right and let's check to
  • 00:13:27 see if it works with like maybe a
  • 00:13:30 hexagon let's just make sure that it's
  • 00:13:31 actually working properly so I can do a
  • 00:13:33 hexagon equals polygon and that's gonna
  • 00:13:36 have six sides and I'll call it hexagon
  • 00:13:42 it's good and now let's do hexagon dot
  • 00:13:47 draw
  • 00:13:49 look at that it's calculating those
  • 00:13:51 angles correctly that's pretty sweet
  • 00:13:53 one thing I think it would be nice to do
  • 00:13:55 is right now we're passing in a hundred
  • 00:13:58 always here we could pass this in as
  • 00:14:00 another variable to our polygon and so
  • 00:14:05 we'll do size and we'll say self dot
  • 00:14:07 size equals size so now when we define
  • 00:14:10 these we have to pass in like 100 here
  • 00:14:13 if we don't pass in 100 see what happens
  • 00:14:16 it gives me this long error and
  • 00:14:18 ultimately this is the important part of
  • 00:14:20 the air it's missing their one required
  • 00:14:22 positional argument sighs so it's airing
  • 00:14:26 out because we didn't pass in size and
  • 00:14:28 we included that in the init method so
  • 00:14:30 it doesn't know what to do so we can
  • 00:14:32 pass in 100 here and 100 here and that
  • 00:14:37 would like allow us to define those
  • 00:14:38 properly we're gonna get the error again
  • 00:14:40 down here
  • 00:14:41 so let's say we wanted to find it that's
  • 00:14:43 10 to start and we've passed now in our
  • 00:14:46 draw method we should do self dot size
  • 00:14:48 instead of 100 so let's see what happens
  • 00:14:51 look at that it's 10 is a little tiny
  • 00:14:54 hexagon and if we did 100 should be a
  • 00:14:58 bigger hexagon and it is cool cool but
  • 00:15:02 maybe it's like annoying too you can
  • 00:15:04 imagine we could just keep building out
  • 00:15:06 the number of parameters we want to pass
  • 00:15:08 into our polygons and would be annoying
  • 00:15:11 to have to like include every single one
  • 00:15:13 when we define our classes so what we
  • 00:15:17 can do is the same as we can do with
  • 00:15:18 functions is we can give it a default
  • 00:15:21 argument so let's say our default
  • 00:15:23 argument is 100 so now I can define it
  • 00:15:26 without these parameters and do it for
  • 00:15:33 all three and now it won't error out on
  • 00:15:39 me because it'll just know that if I
  • 00:15:41 don't provide this size it will default
  • 00:15:43 it to 100 so watch what happens when we
  • 00:15:45 draw the hexagon see it's the normal big
  • 00:15:48 hexagon so if I go ahead and make that
  • 00:15:50 10 here it will override that default
  • 00:15:53 value of 100 and allow us to draw it at
  • 00:15:56 size 10 and it works cool and you can
  • 00:16:00 imagine where you start getting fancy
  • 00:16:02 with the type of things we pass in here
  • 00:16:04 so maybe we also want to be able to
  • 00:16:05 provide a color for our polygons so
  • 00:16:07 let's say the default color is black we
  • 00:16:12 can use turtle then this is just
  • 00:16:14 something I know about turtle that it
  • 00:16:15 accepts where it's like that I could do
  • 00:16:17 turtle dot color is self dot color and I
  • 00:16:22 need to define self dot color here
  • 00:16:27 and now it will draw the shape based on
  • 00:16:30 the color we select so we have black
  • 00:16:35 line there and if I wanted to do a
  • 00:16:39 different color I could pass in I can
  • 00:16:41 either pass in both the size and the
  • 00:16:43 color or I could just define that I want
  • 00:16:45 the color to equal let's say red and as
  • 00:16:50 you see it's red and you can imagine I
  • 00:16:53 could you know maybe pass in the
  • 00:16:54 thickness to so thick line thickness
  • 00:16:59 that's going to equal let's say like two
  • 00:17:04 pixels to start by default and within
  • 00:17:08 turtle I think there's a way to set the
  • 00:17:09 line thickness turtle dot let's see if
  • 00:17:11 we can use a lot utilize this
  • 00:17:13 documentation what other things screen
  • 00:17:14 choice and look at this all this
  • 00:17:18 documentation does a lot of helpful
  • 00:17:20 things pen size that seems good to me so
  • 00:17:24 I can do turtle dot pen size and
  • 00:17:28 probably pass in self dot size let's see
  • 00:17:31 what happens when I draw oh my god what
  • 00:17:35 is that what is the size of this oh my
  • 00:17:37 god Oh oops I did self dot size here
  • 00:17:40 instead of self dot a line thickness
  • 00:17:44 okay so I'm going to pass in self line
  • 00:17:47 thickness equals line thickness that was
  • 00:17:52 a clacks classic mix-up okay and now we
  • 00:17:55 could pass in you know maybe it would
  • 00:17:58 make three the default value and I think
  • 00:18:00 this is in pixels but if we wanted a
  • 00:18:02 really thick hexagon I could pass in you
  • 00:18:04 know line thickness equals twenty and as
  • 00:18:11 you see you get a thicker polygon so we
  • 00:18:13 can do all sorts of cool stuff but maybe
  • 00:18:15 we just want the tie to fall to just
  • 00:18:17 have to pass in the you know number of
  • 00:18:21 sides in the name just to drill down a
  • 00:18:24 point on what the heck self is let's
  • 00:18:26 imagine we had to write this draw method
  • 00:18:28 outside of the outside of a function so
  • 00:18:32 we could do let's define draw function
  • 00:18:35 so in this we needed a color we need a
  • 00:18:39 thickness we need sides we need size we
  • 00:18:41 need an angle so we need a bunch of
  • 00:18:43 parameters so I could say sides size
  • 00:18:49 angle line thickness and color all is
  • 00:18:53 parameters to the function and then I
  • 00:18:55 could basically copy and paste this in
  • 00:19:01 and now instead of you know self we do
  • 00:19:05 this you know we're not using stuff
  • 00:19:11 anymore we're using these parameters you
  • 00:19:12 passed in and you know this still works
  • 00:19:16 so we could still call instead of doing
  • 00:19:19 draw down here we could call something
  • 00:19:22 like draw function five sides size 20
  • 00:19:30 angle I guess this is now not pre
  • 00:19:34 computed so I don't have to figure out
  • 00:19:35 the angle will would be 108 I guess line
  • 00:19:38 thickness for color red we can do this
  • 00:19:43 and it's still gonna work for us the
  • 00:19:45 thing is it's just really annoying not
  • 00:19:47 being middle to utilize like something
  • 00:19:49 we've already kind of predefined pre
  • 00:19:50 computed some things it just makes our
  • 00:19:52 life more difficult to not use the class
  • 00:19:55 method so self is really helping us by
  • 00:19:58 allowing us to access all these things
  • 00:20:00 that we initialize at the creation of
  • 00:20:02 our class and a nice succinct manner all
  • 00:20:06 right we can delete this because I was
  • 00:20:07 just using it as an example and also I'm
  • 00:20:10 gonna have all this code on my github
  • 00:20:12 page and that's link to in the
  • 00:20:13 description but for the time being I'm
  • 00:20:15 going to just delete some of this stuff
  • 00:20:16 to kind of make the code a little bit
  • 00:20:17 cleaner for now all right the next topic
  • 00:20:19 we're going to talk about is the concept
  • 00:20:20 of inheritance and specifically we're
  • 00:20:22 gonna be talking about sub classing so
  • 00:20:25 let's say that we wanted to define a
  • 00:20:26 class that was specifically a square but
  • 00:20:29 we didn't want to have to rewrite a lot
  • 00:20:30 of this logic to draw a square and you
  • 00:20:33 know would you define the number of
  • 00:20:34 sides of what not a square has we can
  • 00:20:36 utilize this polygon it's kind of the
  • 00:20:38 parent class and make a subclass called
  • 00:20:41 square that utilizes it so we can do
  • 00:20:43 class square and that's going to be a
  • 00:20:46 subclass of polygon so we have the past
  • 00:20:49 polygons to specify that square is
  • 00:20:52 utilizing polygons and what we can do
  • 00:20:56 now is we can define it an it method for
  • 00:21:00 the square so we can say something like
  • 00:21:04 definite just like we did before we're
  • 00:21:08 gonna pass itself like we always do and
  • 00:21:10 if we want we can pass in all these same
  • 00:21:13 parameters or actually we and then we're
  • 00:21:21 going to decide what parameters we have
  • 00:21:22 to pass in well for a square we know
  • 00:21:27 that the sides is only going to be
  • 00:21:28 forced we don't need to pass that in
  • 00:21:29 each time we define a square we notice
  • 00:21:31 you may call to square so we don't need
  • 00:21:32 to pass that in so we can just utilize
  • 00:21:34 these last three parameters and pass
  • 00:21:36 those in and then to specify that these
  • 00:21:40 always have four sides and always are
  • 00:21:42 called squares we can use the super
  • 00:21:45 method in Python so we can do super and
  • 00:21:48 that's basically say saying take from
  • 00:21:50 polygon anything from the parent class
  • 00:21:53 the superclass we're gonna utilize it
  • 00:21:56 using this super method and when you do
  • 00:21:58 super dot an it so we're getting the
  • 00:22:01 initialization method from polygon and
  • 00:22:04 we're gonna pass in four for the number
  • 00:22:06 of sides we're gonna pass in the name
  • 00:22:08 square and then we're gonna pass in all
  • 00:22:10 the other stuff so size color line
  • 00:22:16 thickness etc okay and now we've
  • 00:22:21 initialized that so one thing that's
  • 00:22:23 really cool is just by utilizing this
  • 00:22:26 now we can define a square and get
  • 00:22:28 access to all the properties that we
  • 00:22:31 normally have in our polygon so let's
  • 00:22:33 define a square so square equals square
  • 00:22:38 now and I can just do that I don't have
  • 00:22:40 to pass in the number of sides or the
  • 00:22:42 name anymore because we're creating a
  • 00:22:44 subclass of polygon called square and
  • 00:22:48 does that give us an error it doesn't
  • 00:22:49 give us an error and watch we can even
  • 00:22:51 use like squared out sides we can print
  • 00:22:53 this out
  • 00:22:57 and we can print out let's say squared
  • 00:23:01 angle so this should be 90 so it should
  • 00:23:04 be four sides and angle 90 and look at
  • 00:23:06 that just this thing utilize the
  • 00:23:09 initialization method of the parent
  • 00:23:11 class and we were able to get the angles
  • 00:23:14 and you know define the sides through
  • 00:23:17 this so this is kind of a neat little
  • 00:23:19 trick and subclassing allows us to like
  • 00:23:21 get very complex with our code because
  • 00:23:24 we can create these structures that
  • 00:23:26 reuse code instead of having to rewrite
  • 00:23:28 it from scratch so subclassing allows us
  • 00:23:31 to you know really utilize previously
  • 00:23:34 written code and not get too verbose and
  • 00:23:36 you know rewrite things we've already
  • 00:23:38 written so if we have a mistake one spot
  • 00:23:40 usually fixing it in that one spot we'll
  • 00:23:43 fix it all around so this is a nice
  • 00:23:44 little thing about sub classing and to
  • 00:23:48 drive this point home we could even use
  • 00:23:49 square draw and get access to that from
  • 00:23:52 the initialization method and from this
  • 00:23:54 being a subclass so squared out draw
  • 00:23:57 look at that we got a square and if I
  • 00:23:59 wanted to I could pass in you know like
  • 00:24:01 color equals let's say we wanted
  • 00:24:04 hexadecimal colors of 1 2 3 ABC I don't
  • 00:24:07 know what color this will be we'll find
  • 00:24:09 out so that gives us some sort of blue
  • 00:24:12 we could pass in you know a but try to
  • 00:24:17 pass in sides equal 5 see what happens
  • 00:24:19 and it's going to give us an error
  • 00:24:21 because that's not a part of the
  • 00:24:23 initialization method of the square so
  • 00:24:25 it's kind of nice it keeps people from
  • 00:24:27 trying to like trick the system and to
  • 00:24:29 find a polygon with sides four of a name
  • 00:24:31 like Pentagon let's say do size equals
  • 00:24:37 200 big big big square cool this is
  • 00:24:42 awesome
  • 00:24:43 another useful thing to know about sub
  • 00:24:45 classing is method overriding
  • 00:24:47 so let's say we wanted to take the
  • 00:24:49 polygon draw method and do a little bit
  • 00:24:52 more to it so we can actually do in our
  • 00:24:53 subclass is override that draw method so
  • 00:24:56 we can do ja def draw self and maybe we
  • 00:25:01 wanted to fill in not only color it
  • 00:25:04 based like like this but actually fill
  • 00:25:06 in that color
  • 00:25:07 I'm gonna do turtle dot begin Phil this
  • 00:25:10 is just a turtle method you're gonna
  • 00:25:11 watch my video on tutorial on turtle if
  • 00:25:13 you want to know all about it
  • 00:25:15 turtle about begin Phil and then we're
  • 00:25:17 gonna use super dot draw so we're going
  • 00:25:20 to copy and paste basically the code
  • 00:25:22 from up here in the middle and then
  • 00:25:24 turtle dot and Phil and just because I
  • 00:25:28 know that we're gonna get a problem
  • 00:25:29 because I left this turtle down in here
  • 00:25:31 I'm gonna remove this from the function
  • 00:25:33 and actually put turtle dot on down here
  • 00:25:36 and that just keeps my screen open when
  • 00:25:38 I'm done drawing so let's see what
  • 00:25:40 happens now when I do turtle dot or
  • 00:25:41 begin Phil look at that we surrounded
  • 00:25:45 the normal method the draw method from
  • 00:25:48 here basically we used it in our sub
  • 00:25:51 class and added a begin fill in an end
  • 00:25:53 fill and now we get some like extra
  • 00:25:55 functionality which is pretty cool okay
  • 00:25:58 that will cover sub classing let's move
  • 00:26:00 on to another example that will cover a
  • 00:26:02 couple more concepts with classes before
  • 00:26:04 we end this video in this example we're
  • 00:26:07 going to define a point class that we'll
  • 00:26:09 use to cover a couple different concepts
  • 00:26:11 so to find that we can start by doing
  • 00:26:13 class point and we'll need to initialize
  • 00:26:17 some things for this and so anytime we
  • 00:26:20 define a point we can think of these as
  • 00:26:21 being two-dimensional points so we need
  • 00:26:23 an x and a y I'm just going to set them
  • 00:26:25 like this self dot x equals x and self
  • 00:26:29 dot y equals y and I can go ahead and
  • 00:26:33 write there where you have a point class
  • 00:26:35 I can say ay or like let's say point one
  • 00:26:39 equals point four five and if I did
  • 00:26:44 point print point one dot X we'll get an
  • 00:26:51 error I don't know what I did here what
  • 00:26:54 did I do
  • 00:26:54 oh we got a pass himself there we go and
  • 00:26:59 we get four cool so the next thing you
  • 00:27:02 might want to add to a point class is
  • 00:27:04 maybe we want some way to plot this
  • 00:27:06 point to do that we'll import matplotlib
  • 00:27:15 and we will do the following so we'll
  • 00:27:19 define a point dot plot so def plot that
  • 00:27:22 will just take itself and we can do a
  • 00:27:24 plot dot scatter I think we can just
  • 00:27:28 pass in self dot X self dot Y here and I
  • 00:27:34 think if we just do that let's see what
  • 00:27:37 happens so I'll say a equals point three
  • 00:27:41 for a dot plot and then I think we have
  • 00:27:47 to do plot dot show and actually see a
  • 00:27:49 graph and we got a point right there
  • 00:27:53 cool ok so we have a point and we can
  • 00:27:55 plot the point that's cool what if we
  • 00:27:57 wanted to do something fancy let's say
  • 00:27:59 we define two points so we have one
  • 00:28:02 point that's one one and we'll define B
  • 00:28:05 as the point two two so let's say we
  • 00:28:09 wanted to be able to make a third point
  • 00:28:12 C that was equal to a plus B let's see
  • 00:28:15 what happens when we try to run that and
  • 00:28:17 it gives us an error
  • 00:28:19 what is our error well it says
  • 00:28:22 unsupported operand types
  • 00:28:24 four plus point end point so it doesn't
  • 00:28:27 know how to add this point class that we
  • 00:28:29 defined so this gets us into the concept
  • 00:28:32 of operator overloading so we can
  • 00:28:35 actually override the default like plus
  • 00:28:38 for the point class and so we can
  • 00:28:40 actually make it know how to add a plus
  • 00:28:43 B even though it's not like a built-in
  • 00:28:45 Python type so to do that we can define
  • 00:28:48 another kind of special type in the
  • 00:28:50 class called under underscore add so it
  • 00:28:53 has a similar syntax to the unit and it
  • 00:28:58 we can do self
  • 00:28:59 comma other so basically if we imagine
  • 00:29:03 that the other is a point type if we do
  • 00:29:08 cell or like x equals self dot X plus
  • 00:29:14 other x and y equals self dot y plus
  • 00:29:19 other dot Y so this is assuming other is
  • 00:29:23 another point then we can return
  • 00:29:26 a new point which is these new
  • 00:29:29 coordinates x and y so X is being the
  • 00:29:32 addition of these and Y being the
  • 00:29:34 addition of these so let's see if that
  • 00:29:37 works no errors so the moment of truth
  • 00:29:42 comes when we try to print out something
  • 00:29:44 part of C so C dot Y and C dot X should
  • 00:29:49 both be three and is that the case look
  • 00:29:53 at that three and three
  • 00:29:55 looks good and real quick I think it's
  • 00:29:57 useful to see we can overload all sorts
  • 00:30:00 of different operators in Python so we
  • 00:30:02 just over rode the add operator we can
  • 00:30:06 overload tons of different stuff so I'm
  • 00:30:08 gonna bring in a helpful little cheat
  • 00:30:09 sheet and I'll post this in the
  • 00:30:12 description of the video but as you can
  • 00:30:14 see you know you got the binary
  • 00:30:15 operators we can do all sorts of stuff
  • 00:30:17 division for division modular we can do
  • 00:30:21 greater than signs greater than or equal
  • 00:30:24 greater than or equal to signs all sorts
  • 00:30:27 of interesting stuff here and this is
  • 00:30:30 only like a sub list of everything that
  • 00:30:33 you can actually do building on this
  • 00:30:35 let's say I wanted to do maybe we'll
  • 00:30:40 change up our points so instead of doing
  • 00:30:42 a and B we'll do we'll just define a as
  • 00:30:45 being maybe zero two and let's say we
  • 00:30:54 wanted to create a new point I'm going
  • 00:30:56 to just call this point D which is equal
  • 00:30:58 to a plus 5 well 5 is now no longer a
  • 00:31:04 point so if I try to do this what did I
  • 00:31:07 do
  • 00:31:08 if I try to do this we're gonna get an
  • 00:31:10 error and into object has no attribute X
  • 00:31:14 so it like can't do this command so we
  • 00:31:18 can account for this we can allow
  • 00:31:20 ourselves to add different types to the
  • 00:31:22 point class so what we're gonna do to
  • 00:31:25 handle this is we will say if is
  • 00:31:30 instance and we're going to pass in
  • 00:31:33 other and if it's an instance of the
  • 00:31:36 point class
  • 00:31:38 this is how we use it let's check the
  • 00:31:39 documentation for his instance so it's
  • 00:31:41 the object and then I think the type so
  • 00:31:45 if is instance object of point class
  • 00:31:48 then we want to do this
  • 00:31:52 LF maybe honestly we can just kind of
  • 00:31:59 encapsulate all the additions in the
  • 00:32:01 else case we can say X equals self dot X
  • 00:32:05 plus other and y equals self dot y plus
  • 00:32:10 other so the reason I left it as else is
  • 00:32:13 I wanted to be able to handle not only
  • 00:32:15 intz that we're adding to it but maybe
  • 00:32:17 floats so like decimal numbers in
  • 00:32:18 addition to energy numbers and then we
  • 00:32:21 will want to return a point of XY just
  • 00:32:24 like we did in the above case so let's
  • 00:32:28 see what happens now if we do a plus
  • 00:32:30 five didn't give us an error does it
  • 00:32:33 work do X so it should be five here and
  • 00:32:38 D dot y should be seven so let's print
  • 00:32:41 that out real quick let's see if it
  • 00:32:46 works look at that and if we go ahead
  • 00:32:49 and actually add another point to this
  • 00:32:52 we can see that it still works with the
  • 00:32:53 original code so let's say point one one
  • 00:32:56 see we can now add two different types
  • 00:32:59 so that's pretty cool with the operator
  • 00:33:01 overloading alright we're going to end
  • 00:33:03 this video here hopefully you guys feel
  • 00:33:04 like you learned the basics of classes
  • 00:33:06 and kind of how you can use them
  • 00:33:08 effectively
  • 00:33:09 I couldn't cover everything about
  • 00:33:10 classes in single video so if there's
  • 00:33:12 certain things you feel like I left out
  • 00:33:13 let me know in the comments so that I
  • 00:33:15 can include them in a future video if
  • 00:33:17 you enjoyed this video make sure to
  • 00:33:18 throw it a big thumbs up and also if you
  • 00:33:21 haven't already hit that subscribe
  • 00:33:22 button and then the final thing I want
  • 00:33:24 to say before I go is if you thought
  • 00:33:27 kite was cool throughout this video make
  • 00:33:29 sure to check it out the link is in the
  • 00:33:32 description thank you again guys for
  • 00:33:34 watching until next time
  • 00:33:36 Purcell
  • 00:33:40 [Music]