1 00:00:00,500 --> 00:00:04,000 So far we've been building new classes and using classes to add attributes and 2 00:00:04,000 --> 00:00:05,260 methods to our custom classes. 3 00:00:05,260 --> 00:00:08,170 Let's take a break from creating classes though and look at a couple of 4 00:00:08,170 --> 00:00:11,240 useful functions for identifying what kind of objects we're working with. 5 00:00:12,530 --> 00:00:16,750 So, these functions aren't necessarily something you're going to use every day or 6 00:00:16,750 --> 00:00:19,130 even something you're gonna wanna use all the time. 7 00:00:19,130 --> 00:00:22,590 They are, however, really handy when you need to act a certain way or 8 00:00:22,590 --> 00:00:25,650 an object, depending on what kind of object it is. 9 00:00:25,650 --> 00:00:27,110 If Python is duck typed, 10 00:00:27,110 --> 00:00:30,570 consider this to be how you separate the mallards from the mandarins. 11 00:00:30,570 --> 00:00:32,300 So I'm gonna hop into Python, and 12 00:00:32,300 --> 00:00:37,430 I'm just gonna show what these do because they're really fun and interactive. 13 00:00:37,430 --> 00:00:39,840 The first is probably the one that you're going to use the most often. 14 00:00:39,840 --> 00:00:42,160 This function isinstance 15 00:00:42,160 --> 00:00:46,400 tells you whether or not something is an instance of a particular class. 16 00:00:47,690 --> 00:00:51,300 And the string 'a' does happen to be a string. 17 00:00:51,300 --> 00:00:53,630 You can use it with multiple types, too. 18 00:00:53,630 --> 00:00:58,650 So we can say, isinstance, and let's get the float 5.2, 19 00:00:58,650 --> 00:01:03,600 and then inside of a new tuple we can say int or float, and 20 00:01:03,600 --> 00:01:05,820 we get back True because it is either an int or float. 21 00:01:05,820 --> 00:01:06,540 It's a float. 22 00:01:07,640 --> 00:01:15,370 We can also do isinstance 5.2, is that a string, a boolean, or a dict? 23 00:01:16,970 --> 00:01:18,610 No, it's not. 24 00:01:18,610 --> 00:01:21,840 This also does expose a pretty fun little fact from Python's past. 25 00:01:22,890 --> 00:01:28,420 Cuz you can do isinstance True, and int, and it is. 26 00:01:28,420 --> 00:01:32,770 I'll leave it up to you to figure out why or how that came about. 27 00:01:32,770 --> 00:01:36,720 The second function is issubclass, issubclass. 28 00:01:38,270 --> 00:01:39,570 And this one tells you whether or 29 00:01:39,570 --> 00:01:43,790 not a particular class is a subclass of another class. 30 00:01:43,790 --> 00:01:46,380 Like, isinstance, this can also take a list of classes, or 31 00:01:46,380 --> 00:01:48,630 rather a couple of classes to compare against. 32 00:01:48,630 --> 00:01:55,196 So, (bool, int) True issubclass(str, 33 00:01:55,196 --> 00:01:58,820 int) False, okay? 34 00:01:58,820 --> 00:02:02,020 And we can use that to check the tree of our own classes, too. 35 00:02:02,020 --> 00:02:04,720 I'm gonna use Ctrl l here, to clear the screen. 36 00:02:04,720 --> 00:02:11,020 And I'm gonna say from thieves import Thief. 37 00:02:11,020 --> 00:02:15,050 And then I'm gonna say from characters import Character. 38 00:02:16,520 --> 00:02:23,520 And then, I'm gonna say issubclass is Thief is subclass of Character. 39 00:02:23,520 --> 00:02:26,860 True, since our Thief class inherits from the Character class, 40 00:02:26,860 --> 00:02:31,170 our Thief is a subclass of Character, of course you already knew that. 41 00:02:31,170 --> 00:02:35,550 The last two items are a bit less useful but they're still good to know about. 42 00:02:35,550 --> 00:02:37,790 Firstly, we have the type function. 43 00:02:37,790 --> 00:02:40,750 Now, type can be used for some trickier behavior, but 44 00:02:40,750 --> 00:02:43,838 let's just focus on using it to get information about an instance. 45 00:02:43,838 --> 00:02:47,400 So I'm gonna make a new Thief, and 46 00:02:47,400 --> 00:02:49,790 remember I have to put in the name argument now. 47 00:02:51,080 --> 00:02:54,890 And I'm going to say type for kenneth. 48 00:02:54,890 --> 00:02:56,900 And I get that kenneth is a Thief. 49 00:02:56,900 --> 00:02:59,200 Now, we already knew that, right? 50 00:02:59,200 --> 00:03:01,380 But, it's great that Python can tell us. 51 00:03:01,380 --> 00:03:05,500 The type function tells you the type of object that an instance is. 52 00:03:05,500 --> 00:03:08,690 Generally, you'll want to use isinstance, if you're wanting to know if I whether or 53 00:03:08,690 --> 00:03:12,840 not to work with an instance though, since it will check the full inheritance tree. 54 00:03:12,840 --> 00:03:14,520 If I was to use this, to find out whether or 55 00:03:14,520 --> 00:03:17,520 not kenneth was a Character, I wouldn't know. 56 00:03:17,520 --> 00:03:19,840 I only know that kenneth is a Thief. 57 00:03:19,840 --> 00:03:25,130 Now, we've used the __init__ to control how it was created. 58 00:03:25,130 --> 00:03:27,541 Python's classes have tons of these magic methods and 59 00:03:27,541 --> 00:03:30,945 we're gonna talk about several of them in the next stage. 60 00:03:30,945 --> 00:03:34,485 Python classes also have a lot of magic attributes too, and 61 00:03:34,485 --> 00:03:36,875 one of those is the class attribute. 62 00:03:36,875 --> 00:03:40,191 It'll tell you what class an instance is. 63 00:03:40,191 --> 00:03:47,449 So we can say kenneth.__class__ and get back our thieves.Thief. 64 00:03:47,449 --> 00:03:53,146 And we can even go a step further and get the magic name attribute off of the class. 65 00:03:53,146 --> 00:03:59,830 Kenneth.__class__.__name__, and that tells us Thief. 66 00:03:59,830 --> 00:04:03,448 So if you wanted to write some code, then inspect at an instance's class name, 67 00:04:03,448 --> 00:04:04,210 you could do it. 68 00:04:06,005 --> 00:04:09,740 While Python is heavily slanted toward duck typing, where if an object quacks 69 00:04:09,740 --> 00:04:12,500 like a duck and waddles like a duck should be considered a duck. 70 00:04:12,500 --> 00:04:15,710 I mean, if you can use a method on an object and not get an exception, 71 00:04:15,710 --> 00:04:17,380 you should use it that way. 72 00:04:17,380 --> 00:04:20,230 It's still really handy to be able to tell if something is an instance of 73 00:04:20,230 --> 00:04:21,850 a particular class or not. 74 00:04:21,850 --> 00:04:24,710 You'll find yourself using these functions and attributes when you get into code 75 00:04:24,710 --> 00:04:27,630 that operates based on the type of arguments it's working with. 76 00:04:27,630 --> 00:04:31,390 Often referred to metaprograming or introspective programming. 77 00:04:31,390 --> 00:04:34,330 Both of those are beyond the scope of this course, but feel free to look 78 00:04:34,330 --> 00:04:36,880 into them yourself, they're very interesting ways of programming. 79 00:04:37,900 --> 00:04:40,480 All right, take a break if you need it, get a snack if you're hungry, 80 00:04:40,480 --> 00:04:41,470 do code challenges and 81 00:04:41,470 --> 00:04:44,770 quizzes to cement all of this knowledge in your head, then come back for 82 00:04:44,770 --> 00:04:48,410 the next stage where we'll get our hands dirty with Python's built in classes. 83 00:04:48,410 --> 00:04:50,220 Ever wanted dot notation on a dictionary?