Learn the difference between type and instance as well as how to use the type() and isinstance() functions. Type() returns the type of one given input while isinstance() has two inputs and returns true or false whether or not the first input is an instance of the second input. Isinstance() accounts for some things that type() does not - so it is often better to use in situations that involve inheritance.
- [Narrator] Now let's learn about some functions that are going to help us test for certain functionality. The type function takes one input, any object, and the output is the type of the given input. So, it could be int, string, whatever type that object is. So going to the code, let's just create a range, say r, and we're going to have it be the range from zero to 30. We learned about ranges in a previous video, but let's just go ahead and print out the fact that this is a range.
And so here we get class range, because r is of type range. We can also do this for different types of data. So we could print out the type of a number. It's going to be int because 10 is an integer. We can also do this for strings, and so we could go print type A, and that's just one character string, and then we'll have print type Hi there, which is a bit of a longer string, and then printing these out we get str str, which both stand for strings.
We'll see why this is useful later on. The next function we're going to talk about is isinstance, and this takes one object and one class info. The output is a Boolean representing whether or not the object is an instance of the class given by the class info. So X here would be any object, and Y would be some type of class that it's a part of, and so say X is 10, like we did before, then Y would need to be int in order for isinstance to return true.
So let's start off by creating some classes, and so we're going to have a class car, and we're going to implement it later, so we're just going to have pass here for now. And then, we'll have class truck, and it's going to inherit from car, and we're going to implement that later as well. Then we're going to create some variables. We're going to have c which is going to be an instance of car, and we're going to have t which is going to be an instance of truck. And then we'll print out the type of each of these variables, and so we'll print out the type of c and we'll print out the type of t.
And then running these, notice we get two different things, because c is a direct instance of car, we get car as its type, and because t is a direct instance of truck, and not car, even though truck inherits from car, we get truck as its type. Now let's see if these types are the same. So we could go print type c equals type t. Your intuition probably told you this was false, and that's correct.
The type of c is not the same as type t. This is because c is a direct instance of car, and t is a direct instance of truck. Although truck inherits from car, type doesn't really consider inheritance. It's just looking at what it's a direct instance of. A true case of this would be if we had convert as a variable and it was an instance of car, and then we did print type c equals type convert. If we run this, this would be true, because both c and convert are direct instances of car.
There's nothing in between that's inheriting from anything else. Type does not count for inheritance. Now what does count for inheritance? A function called isinstance, and so, to use isinstance, we can do something like print isinstance, and remember, we do the object first, and so we'll say c, and then we'll put the class info, which is just the name of the class, and so car. So we see isinstance is c an instance of car, and this will be true because c is in fact an instance of car.
Now what if we did this for something else? Isinstance, say t, is that an instance of car? We see true, because isinstance accounts for inheritance. Because truck inherits from car, t is in fact an instance of car as well as truck. So, if I replace this with truck here, it will still be true. So we see true. But if we take away the fact that truck inherits from car by deleting this piece of code, and then change truck to car here in our isinstance function, run it, we get false, because now t does not inherit from car because truck the class does not inherit from car.
So why is any of this useful? Well, looking back up at our range function up here, something that we can do with ranges is list them out, list each value inside of them, and we learned this in a previous video, and so we should check before running the list function on a given input if it's an actual range so that the list function running it will be valid. And so, we could go if isinstance, so checking if r is an instance of range. If this is true, then we can go ahead and run the list function, and so we can print out each value in the range with the list, and so running this, it lists out each value in the range because r is an instance of range.
Often, we'll use these types of methods to test for functionality, hence, if the input is a certain type of instance, we can guarantee it has a certain function we can use, or it is an input that is valid for a different function. In most cases, it's better to use isinstance than type, just because it accounts for the inheritance that you might not know about that's going on, behind the scenes.
- Working with logical and comparison operators
- Getting a list of numbers with the range() and list() functions
- Using mathematical functions such as round(), abs(), and pow()
- Calculating a given input's length
- Importing and using the math module
- Reading a user's command-line arguments
- Getting the current time
- Formatting dates and times with datetime
- Creating a timer
- Using urllib to get content from the Internet
- Using the JSON module to decode content