1 00:00:00,870 --> 00:00:02,770 - In this video I'd like to take a moment 2 00:00:02,770 --> 00:00:06,050 to talk about methods which are actually 3 00:00:06,050 --> 00:00:09,090 functions that belong to objects. 4 00:00:09,090 --> 00:00:13,040 So, methods and functions are really the same concept, 5 00:00:13,040 --> 00:00:16,470 the difference is that functions are defined to stand 6 00:00:16,470 --> 00:00:20,780 alone, whereas methods belong to a class. 7 00:00:20,780 --> 00:00:23,890 And, in a later lesson, we'll talk about defining 8 00:00:23,890 --> 00:00:28,330 your own classes, but there are tons of existing classes 9 00:00:28,330 --> 00:00:31,960 that already have been defined for you as part of 10 00:00:31,960 --> 00:00:33,940 the Python standard library. 11 00:00:33,940 --> 00:00:38,490 In fact, we used the decimal class previously to do 12 00:00:38,490 --> 00:00:41,400 monetary calculations, 13 00:00:41,400 --> 00:00:42,850 and in this example, 14 00:00:42,850 --> 00:00:46,420 I'd like to use a string object, which is an instance 15 00:00:46,420 --> 00:00:48,770 of Python's string class, 16 00:00:48,770 --> 00:00:52,160 to demonstrate this concept of methods. 17 00:00:52,160 --> 00:00:55,610 So let's say I have a string called "Hello". 18 00:00:55,610 --> 00:00:59,210 This is actually a string object containing the characters 19 00:00:59,210 --> 00:01:03,280 that you see here and s is simply a variable that refers 20 00:01:03,280 --> 00:01:06,330 to, or points to, if you will, that string object 21 00:01:06,330 --> 00:01:09,320 someplace in memory. Now, when you have an object, 22 00:01:09,320 --> 00:01:14,260 you can quote, unquote, talk to it, by invoking its methods. 23 00:01:14,260 --> 00:01:18,440 So, if I have s, which refers to the object containing 24 00:01:18,440 --> 00:01:23,310 the string Hello, I can use a dot and then I can tell it 25 00:01:23,310 --> 00:01:26,540 to do something. So, the string class that implements 26 00:01:26,540 --> 00:01:29,670 strings in Python, for example, has a function called 27 00:01:29,670 --> 00:01:33,760 lower, which gives you back a lowercase representation 28 00:01:33,760 --> 00:01:36,740 of that string. Now, strings are immutable, 29 00:01:36,740 --> 00:01:40,900 so it doesn't modify the original string, it simply 30 00:01:40,900 --> 00:01:43,960 gives me back a new string containing all of those 31 00:01:43,960 --> 00:01:48,960 letters in lowercase. Similarly, I can do s.upper(), 32 00:01:49,590 --> 00:01:54,100 which will give me back an uppercase version of that string. 33 00:01:54,100 --> 00:01:57,410 Now, for every class that's part of the Python 34 00:01:57,410 --> 00:02:00,760 standard library, when you go to python.org 35 00:02:00,760 --> 00:02:05,060 and look at the Python standard library reference, 36 00:02:05,060 --> 00:02:07,920 you're going to be able to look at each individual class 37 00:02:07,920 --> 00:02:10,020 in a given module and then within 38 00:02:10,020 --> 00:02:13,750 that class's documentation, it's going to show you 39 00:02:13,750 --> 00:02:18,160 the methods that are available to you. 40 00:02:18,160 --> 00:02:20,133 So, for instance, 41 00:02:21,030 --> 00:02:25,190 if I go over to python.org 42 00:02:25,190 --> 00:02:28,293 and we'll pull up the documentation webpage, here. 43 00:02:29,710 --> 00:02:32,320 Right, so here's the Library Reference. 44 00:02:32,320 --> 00:02:36,340 Now, string is a little bit different from other classes 45 00:02:36,340 --> 00:02:41,070 that are out there, in that the type string is built into 46 00:02:41,070 --> 00:02:45,700 the Python language as the type str, so, for the built-in 47 00:02:45,700 --> 00:02:49,940 types you can see this section within the documentation 48 00:02:49,940 --> 00:02:52,710 for the Python standard library, and here's the type 49 00:02:52,710 --> 00:02:57,710 string, str, which we use for processing strings. 50 00:02:57,850 --> 00:03:00,610 And if you scroll down through this page, 51 00:03:00,610 --> 00:03:03,780 you're going to see there's a section on string methods 52 00:03:03,780 --> 00:03:07,240 and then it shows you all the different methods that 53 00:03:07,240 --> 00:03:10,910 you can call. In these first two cases they happen to be 54 00:03:10,910 --> 00:03:13,440 methods that take no arguments, but it gives 55 00:03:13,440 --> 00:03:15,160 descriptions of the methods. 56 00:03:15,160 --> 00:03:18,690 Let's scroll down. Here is an example of a method 57 00:03:18,690 --> 00:03:21,760 that takes an argument, actually, several different 58 00:03:21,760 --> 00:03:24,050 methods that take arguments and when you see 59 00:03:24,050 --> 00:03:28,100 this notation with square brackets, what this means 60 00:03:28,100 --> 00:03:31,430 is that the center function expects an argument 61 00:03:31,430 --> 00:03:34,540 that represents a field width, in this case, 62 00:03:34,540 --> 00:03:37,650 for centering a string in a field width. 63 00:03:37,650 --> 00:03:41,080 But the second argument is optional. So the square 64 00:03:41,080 --> 00:03:44,900 brackets indicate optional arguments in the context 65 00:03:44,900 --> 00:03:48,890 of the online documentation. 66 00:03:48,890 --> 00:03:52,080 In this case, we have a required first argument 67 00:03:52,080 --> 00:03:55,210 and then we have optional second and third arguments 68 00:03:55,210 --> 00:03:58,350 where you can specify the first argument, you can 69 00:03:58,350 --> 00:04:01,030 optionally specify the second argument and not 70 00:04:01,030 --> 00:04:03,790 a third argument, in this case, or you can specify 71 00:04:03,790 --> 00:04:06,950 three arguments. Notice the nested use of square 72 00:04:06,950 --> 00:04:09,680 brackets in that particular piece of 73 00:04:09,680 --> 00:04:11,290 online documentation. 74 00:04:11,290 --> 00:04:13,860 So, if you really want to get a sense of everything 75 00:04:13,860 --> 00:04:18,790 that you can do for a string in Python, you need to 76 00:04:18,790 --> 00:04:20,940 take a look at this page that I'm showing you 77 00:04:20,940 --> 00:04:23,800 right now, so that you can see all the different 78 00:04:23,800 --> 00:04:26,040 functions that are available to you 79 00:04:26,040 --> 00:04:29,670 and you can get a sense of everything that you can do. 80 00:04:29,670 --> 00:04:31,840 I would definitely recommend at least reading 81 00:04:31,840 --> 00:04:34,330 the names of all the functions, generally they're 82 00:04:34,330 --> 00:04:36,650 named in a way where you can kinda figure out 83 00:04:36,650 --> 00:04:39,080 what they do without having to fully read 84 00:04:39,080 --> 00:04:42,210 the documentation, but for any class you're going 85 00:04:43,846 --> 00:04:46,320 to use, excuse me, you are going to wanna take a 86 00:04:46,320 --> 00:04:49,310 close look at its online documentation so you 87 00:04:49,310 --> 00:04:52,133 really know everything the class can do.