Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Complex Relationships3:37 with Kenneth Love
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
- Child or Sub class: the class that inherits from a particular class.
class Symbol: pass class Letter(Symbol): pass class Alpha(Letter): pass
Letter has two superclasses:
Symbol inherits from
object even though we didn't explicitly state it).
Letter has one subclass,
An instance of the class
Alpha could use any attributes or methods that were defined on
Letter. Those two classes, though, wouldn't have access to attributes or methods that belonged to
Alpha. Inheritance is a one-way street.
If there's one programmer stereotype that I generally agree with,
it's that we're a lazy bunch of people.
Now that's not to say that we don't get our work done.
We just don't like to do the same work multiple times,
at least not if we can help it.
We've even turned this mentality into a catchy acronym, DRY or
don't repeat yourself.
Thankfully, a core concept of object-oriented programming is aimed
directly at reducing the amount of repetition you have to do.
It's called inheritance.
At its simplest, it means we can give a child or subclass all of the abilities and
attributes of a parent or super class.
Python actually allows us to inherit from multiple super classes, but
we'll get there when we get there.
For now though, let's get to workspaces and
check out the fastest way to get rich inheritance.
It's really unlikely that we don't have thieves in a game, right?
We probably have wizards and warriors and other types.
So let's move most of what we have here out into a character class.
So, we'll make a new class.
And we'll call this character, and
we'll say definite, self, name and
kwargs and self.name= name and
for key, value in kwargs.items.
Setattr self key value, okay cool.
So now we'll make our Thief class inherit from character and
we signify that inheritance by doing these parentheses and a name, okay?
Now we're gonna lose a little bit of our stuff, but we'll bring it back sooner,
So we can keep the sneaky, let's take out the init, and
we can even keep the pickpocket and the hide.
Okay, but just for now they're always sneaky.
Okay, so now we have our thief attribute,
we can't set the sneaky or that kind of stuff.
Now we get back the pickpocket and
the hide because we've defined them in our class.
So, since we have thief inherit from character,
Thief gets all the stuff that character does.
But Thief can add on its own stuff as well, as long as there's no
name collisions, things that are named the same on both of them.
That's fine, they'll live happily next to each other.
And if there are collisions, we'll talk about this a bit more in a bit.
Now, we don't have the ability to set sneakiness anymore, right?
We were doing that through the init.
We'll cover that in the next video cuz it requires a bit more explanation.
For now though, let's just see if our class is working more or
less like it was before, okay.
Save that, pop down here, python from
characters import Thief, kenneth = Thief with the name of Kenneth.
And if we do kenneth.name, we still get Kenneth.
And if we do kenneth.sneaky, we get True, because that's always set.
Let's do kenneth.sneaky = False and then kenneth.pickpocket.
And we get False, that should still work.
Everything seems to be just like we had it before.
Inheritance can save us a bunch of time?
Something I want to point
out is that we've been using inheritance this whole time.
Every class extends from a built-in class called object.
In legacy Python, you had to specify this inheritance yourself or things got weird.
But with Python 3, the language designers improved the class creation process, so
now everything automatically inherits from object.
All right, time to try a code challenge and
then come back to learn about the super function.
You need to sign up for Treehouse in order to download course files.Sign up