Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Classes are great, but they're even better with parents.
Inheritance in Python is both easier and harder than in some other languages. Python's inheritance usually works exactly how you expect it to. The child classes, or subclasses, get all of the attributes and methods of their parents and grandparents and so on. But thanks to things like the "method resolution order (MRO)", which we'll talk about later, sometimes it's not that simple. But let's not worry about that just yet! Make sure you understand these terms before moving on:
- Parent or Super class: the class that a class inherits from. These can go on for a long way, too, so be sure to consider grandparent and great-grandparent classes, too.
All classes have the ultimate ancestor of object
.
- Child or Sub class: the class that inherits from a particular class.
Consider this:
class Symbol:
pass
class Letter(Symbol):
pass
class Alpha(Letter):
pass
The class Letter
has two superclasses: Symbol
and object
(since Symbol
inherits from object
even though we didn't explicitly state it). Letter
has one subclass, Alpha
.
An instance of the class Alpha
could use any attributes or methods that were defined on Symbol
or Letter
. Those two classes, though, wouldn't have access to attributes or methods that belonged to Alpha
. Inheritance is a one-way street.
-
0:00
[MUSIC]
-
0:04
If there's one programmer stereotype that I generally agree with,
-
0:07
it's that we're a lazy bunch of people.
-
0:09
Now that's not to say that we don't get our work done.
-
0:11
We just don't like to do the same work multiple times,
-
0:14
at least not if we can help it.
-
0:15
We've even turned this mentality into a catchy acronym, DRY or
-
0:19
don't repeat yourself.
-
0:21
Thankfully, a core concept of object-oriented programming is aimed
-
0:24
directly at reducing the amount of repetition you have to do.
-
0:26
It's called inheritance.
-
0:28
At its simplest, it means we can give a child or subclass all of the abilities and
-
0:32
attributes of a parent or super class.
-
0:35
Python actually allows us to inherit from multiple super classes, but
-
0:38
we'll get there when we get there.
-
0:39
For now though, let's get to workspaces and
-
0:41
check out the fastest way to get rich inheritance.
-
0:45
It's really unlikely that we don't have thieves in a game, right?
-
0:47
We probably have wizards and warriors and other types.
-
0:51
So let's move most of what we have here out into a character class.
-
0:57
So, we'll make a new class.
-
1:01
And we'll call this character, and
-
1:06
we'll say definite, self, name and
-
1:10
kwargs and self.name= name and
-
1:14
for key, value in kwargs.items.
-
1:19
Setattr self key value, okay cool.
-
1:22
So now we'll make our Thief class inherit from character and
-
1:28
we signify that inheritance by doing these parentheses and a name, okay?
-
1:35
Now we're gonna lose a little bit of our stuff, but we'll bring it back sooner,
-
1:40
I promise.
-
1:41
So we can keep the sneaky, let's take out the init, and
-
1:47
we can even keep the pickpocket and the hide.
-
1:50
Okay, but just for now they're always sneaky.
-
1:53
Okay, so now we have our thief attribute,
-
1:56
we can't set the sneaky or that kind of stuff.
-
1:59
Now we get back the pickpocket and
-
2:00
the hide because we've defined them in our class.
-
2:05
So, since we have thief inherit from character,
-
2:08
Thief gets all the stuff that character does.
-
2:11
But Thief can add on its own stuff as well, as long as there's no
-
2:16
name collisions, things that are named the same on both of them.
-
2:19
That's fine, they'll live happily next to each other.
-
2:22
And if there are collisions, we'll talk about this a bit more in a bit.
-
2:25
Now, we don't have the ability to set sneakiness anymore, right?
-
2:28
We were doing that through the init.
-
2:29
We'll cover that in the next video cuz it requires a bit more explanation.
-
2:34
For now though, let's just see if our class is working more or
-
2:37
less like it was before, okay.
-
2:39
Save that, pop down here, python from
-
2:45
characters import Thief, kenneth = Thief with the name of Kenneth.
-
2:53
And if we do kenneth.name, we still get Kenneth.
-
2:55
And if we do kenneth.sneaky, we get True, because that's always set.
-
2:59
Let's do kenneth.sneaky = False and then kenneth.pickpocket.
-
3:06
And we get False, that should still work.
-
3:08
Everything seems to be just like we had it before.
-
3:11
Inheritance can save us a bunch of time?
-
3:14
Something I want to point
-
3:15
out is that we've been using inheritance this whole time.
-
3:17
Every class extends from a built-in class called object.
-
3:21
In legacy Python, you had to specify this inheritance yourself or things got weird.
-
3:26
But with Python 3, the language designers improved the class creation process, so
-
3:29
now everything automatically inherits from object.
-
3:32
All right, time to try a code challenge and
-
3:34
then come back to learn about the super function.
You need to sign up for Treehouse in order to download course files.
Sign up