Intermediate Python

Types, isinstance, & issubclass

Nina Zakharenko

Nina Zakharenko

Microsoft
Intermediate Python

Check out a free preview of the full Intermediate Python course

The "Types, isinstance, & issubclass" Lesson is part of the full, Intermediate Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina reinforces what was learned by testing knowledge about what types different objects are, and introduces a function that checks if an object is a particular instance of a class (or subclass of the class). all() and any() are also briefly covered.

Preview
Close

Transcript from the "Types, isinstance, & issubclass" Lesson

[00:00:00]
>> Nina: Just like we've been using dir, and help, and type, on the REPL there are a few really helpful methods associated with instances and classes as well. So we can check the type, like we've been doing. So cars.Car, that's a type, the type(my_subaru), is this class itself, right?

[00:00:35]
So just like we did with int, the type of int is a class type. The type of an instance of int is the class itself. There's a built in method called isInstance to help us answer the question of, is this particular instance of this particular type. So this is just built in, you don't have to import it from any where.

[00:01:01]
I can ask is the instance 42 of type int, I'll get True. IsInstance of hello world, is this a string?
>> Speaker 2: Yes.
>> Nina: Yes, IsInstance 42 a string? No, it's not. IsInstance my_subaru, an int? No, if I ask isinstance(my_subaru, cars.Car), it's true, right? We saw that earlier when we looked at the types.

[00:01:47]
>> Nina: Just kind of a funny, interesting factoid here, don't use this in production code. If I check isInstance of a boolean with int, I'll see true.
>> Nina: So we talked about how the boolean, the truthiness value of 0 is false. The truthiness of any number but 0 is true.

[00:02:22]
Booleans were added in Python, later on, after integers. And they wanted to kind of make sure that this new language feature minimized disruption. And so booleans are a subclass of int.
>> Nina: And again, don't do this, but you can add them together, it's not a syntax error.
>> Speaker 2: So, Nina, this is why if you have a set and you have 0, 1, True, False, you actually only get 0 and 1 back as the duplicate.

[00:03:01]
>> Nina: Oops, that's not what I wanted to do.
>> Speaker 2: So that's why.
>> Nina: Yeah, don't do this in production code, it's pretty bad.
>> Nina: One place you might find this a little bit useful, this isn't in the course website, just to digress a little bit. There are two really useful methods called all and any.

[00:03:31]
So any you can pass in a list and it'll say,
>> Nina: Are any of these values truthy? And this is really weird, right? It's kind of hard to read. I even find it hard to remember that -1 is truthy in Python, right? So the better way of doing this would be True, False, True, True.

[00:04:02]
And then you can say are any of these true? You can also use all and ask, are all of these true? And you'll get False. So anyway, interesting little factoid. And everything in Python is a subclass of this kind of greater built in type that we don't really use called object.

[00:04:30]
>> Nina: .So integers, strings, booleans, under the head, they're all objects.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now