Lesson 14: isinstance() and when to inherit at all
How do you check what an object was created from? isinstance() answers — and understands inheritance too: a dog is both a Dog and an Animal. From that same check grows the key rule for when to inherit at all: the "is-a" test.
A dog is a kind of animal — so isinstance says "yes" to both questions: "is it a dog?" and "is it an animal?". Just like Danny is both a student and a person.
- isinstance()
- A built-in function checking whether an object was created from a given class — or one of its children.
- the is-a test
- The inheritance rule of thumb: inherit only when you can honestly say "the child is a kind of the parent".