WEBVTT 1 00:00:01.630 --> 00:00:06.580 so continuing on with classes so what we are going to do is use an example to work with a record 2 00:00:06.580 --> 00:00:11.809 collection to show how classes can be used together and I'm also gonna take this 3 00:00:11.809 --> 00:00:15.940 opportunity to talk about doc strings what they are and what information 4 00:00:15.940 --> 00:00:20.259 should be included in them so lets go ahead and create a new class for this 5 00:00:20.259 --> 00:00:25.710 so I'm gonna come over as I always done new Python file and will 6 00:00:25.710 --> 00:00:35.470 call this one song and we'll start by writing the class so.... 7 00:00:35.470 --> 00:00:37.190 ... 8 00:00:37.190 --> 00:00:41.290 .... 9 00:00:43.330 --> 00:00:49.450 .... 10 00:00:54.210 --> 00:00:57.630 .... 11 00:01:00.590 --> 00:01:19.910 .... 12 00:01:19.910 --> 00:01:35.530 ...so that is our doc string 13 00:01:35.530 --> 00:01:40.930 ...so lets create our __init__ which I said wasn't going to 14 00:01:40.930 --> 00:01:48.700 say the underscore underscore again so.... 15 00:01:48.700 --> 00:01:53.119 .... 16 00:01:53.119 --> 00:02:00.289 ...and lets write the doc string for the init method.... 17 00:02:03.149 --> 00:02:59.239 .... 18 00:02:59.239 --> 00:03:08.530 ....then we need to initialise our data attributes so... 19 00:03:08.530 --> 00:03:19.330 .... 20 00:03:19.330 --> 00:03:25.980 ...so their really shouldn't be much to say at this point about this class and that's 21 00:03:25.980 --> 00:03:30.420 because the doc string should say it all and in fact the doc strings in this class are 22 00:03:30.420 --> 00:03:33.640 probably a little bit to verbose but I just wanted to show you that anyway 23 00:03:34.489 --> 00:03:47.060 ....so their really 24 00:03:47.060 --> 00:03:50.640 shouldn't be too much to say about this class and that's because the doc string 25 00:03:50.640 --> 00:03:54.079 should really say it at all and in fact what I've done I probably have gone overboard 26 00:03:54.079 --> 00:03:58.870 here and made the doc strings a little bit too verbose here so doc strings 27 00:03:58.870 --> 00:04:02.489 themselves can be used to document modules functions classes and methods 28 00:04:02.489 --> 00:04:06.239 and really should provide information that is useful to understanding what the 29 00:04:06.239 --> 00:04:11.970 object does and also how to use it now you should also include other details that 30 00:04:11.970 --> 00:04:16.829 aren't obvious such as what will happen if a value wasn't specified now writing good 31 00:04:16.829 --> 00:04:21.280 documentation is a skill in itself and it can be very difficult to document your own code 32 00:04:21.280 --> 00:04:25.560 and because you know how it works and fits together well you did create it after 33 00:04:25.560 --> 00:04:29.990 all so large companies just out of interest will often employ technical writers to produce 34 00:04:29.990 --> 00:04:34.840 the documentation so someone else who creates a documentation who didn't create the 35 00:04:34.840 --> 00:04:38.770 original program so a description of doc stings and guidelines for writing them 36 00:04:38.770 --> 00:04:41.770 is in pep 257 so if we look at that 37 00:04:42.790 --> 00:04:51.030 paste it on the screen the links are in the Resources section so the pep documents and semantics and 38 00:04:51.030 --> 00:04:55.360 conventions associated with python doc strings well thats actually fairly short 39 00:04:55.360 --> 00:04:58.680 documents as you can see it doesn't really go into a lot of details certainly compared to some 40 00:04:58.680 --> 00:05:02.850 other peps there but I definitely recommend that you read it to specially 41 00:05:02.850 --> 00:05:05.720 as you can skip the final section on handling 42 00:05:05.720 --> 00:05:12.380 doc string indentation that's this bit here so you don't need to worry about that too much getting 43 00:05:12.380 --> 00:05:17.230 back to our code so a doc string should always use triple quotes and that keeps 44 00:05:17.230 --> 00:05:21.690 your doc strings consistent with everyone else's and also means you can include quotes in the 45 00:05:21.690 --> 00:05:24.460 documentation as well now if 46 00:05:24.460 --> 00:05:29.630 you intend to use escape characters you know such as /t or /n in your doc 47 00:05:29.630 --> 00:05:35.860 string then use r with 3 quotes to make it a raw string and lets just see what I mean so I'm going to create a new 48 00:05:35.860 --> 00:05:49.770 class new file and call this one demo I just wanna show you how this works so.... 49 00:05:49.770 --> 00:06:07.690 .... 50 00:06:07.690 --> 00:06:13.540 ...and will print that out to see if that it's working so if we run that and run demo 51 00:06:13.540 --> 00:06:19.340 string split and tab you can see its gone to the next line as well but I just want to show you how to create this raw string 52 00:06:19.340 --> 00:06:28.620 that I just talk about so... 53 00:06:28.620 --> 00:06:33.170 you notice how IntelliJ automatically added the second one and if I take the contents down 54 00:06:33.170 --> 00:06:37.910 here in the first string so I'm actually copying and pasting that I'm gonna paste that 55 00:06:37.910 --> 00:06:50.720 into their and if we print the raw string and run this is just to see the difference so as 56 00:06:50.720 --> 00:06:54.140 you can see by pasting that in there like that what I've done is we've got 57 00:06:54.140 --> 00:07:02.770 Python to not treat it with those escape characters so the /n on line 1and the /t's we're 58 00:07:02.770 --> 00:07:06.970 treated correctly by Python and treated as the carriage return and also 59 00:07:06.970 --> 00:07:10.860 the tab characters but when we put this in this raw string the /n was 60 00:07:10.860 --> 00:07:16.350 ignored and just was treated as a regular character so raw string saw are useful when you don't want Python to treat 61 00:07:16.350 --> 00:07:21.250 the backslash escaped character in a special way so when the string literal 62 00:07:21.250 --> 00:07:25.730 is defined without the r prefix as you saw these /n causes a 63 00:07:25.730 --> 00:07:30.660 line break and /t causes tabs to be included in the string and that's the output at the bottom left of 64 00:07:30.660 --> 00:07:34.510 the screen so I really just provided this as a convenience other ways you have to enter 65 00:07:34.510 --> 00:07:38.450 the ASCII codes for those characters which not only make the code messy but 66 00:07:38.450 --> 00:07:41.950 they cause problems on different operating systems so if Python didn't 67 00:07:41.950 --> 00:07:45.669 provide a mechanism for escaping certain characters we have to probably create 68 00:07:45.669 --> 00:07:50.450 strings like this is so lets just write some more code so... 69 00:07:51.010 --> 00:08:01.320 .... 70 00:08:01.840 --> 00:08:14.680 .... 71 00:08:14.680 --> 00:08:24.410 ..and we got the same response same output as 72 00:08:24.410 --> 00:08:30.870 we got for a sting when we printed that out and of course that's sort of shows you that by 73 00:08:30.870 --> 00:08:35.630 Python letting us put a /n and a /t in that format it saves us from 74 00:08:35.630 --> 00:08:39.599 having to write a string in this format having to add this characters 75 00:08:39.599 --> 00:08:44.159 via ASCII codes to create a string that way so in other words it's really adding 76 00:08:44.159 --> 00:08:47.660 that as a inconvenience for us a shortcut to put those escape characters and if you 77 00:08:47.660 --> 00:08:51.460 wanted to and again using a raw string with the r in front of it enables us to 78 00:08:51.460 --> 00:08:56.210 effectively ignore those escape characters but it gets worse with 79 00:08:56.210 --> 00:09:00.279 these characters because not only was it annoying to type because obviously a lot 80 00:09:00.279 --> 00:09:04.480 more code than in the first example on line 1 but it's also wrong on 81 00:09:04.480 --> 00:09:09.350 windows and that is because Linux and Mac use a line feed character which is the ASCII 82 00:09:09.350 --> 00:09:14.500 code for ten as you can see on line 7 character 10 to start a new line but 83 00:09:14.500 --> 00:09:19.050 Windows users uses 2 characters a carriage return followed by line feed so 84 00:09:19.050 --> 00:09:23.380 to do the equivalent in Windows its character 13 + character 10 or ASCII 85 00:09:23.380 --> 00:09:28.930 symbol 13 and ASCII 10 now reasons why Windows does this are historical and really 86 00:09:28.930 --> 00:09:32.310 dates back to the time before screens when computers were program using 87 00:09:32.310 --> 00:09:37.170 teletype terminals so it was necessary to return the carriage to the 88 00:09:37.170 --> 00:09:41.830 left hand side and in feed down one line and that explains 89 00:09:41.830 --> 00:09:46.870 Windows Control carriage return line feed combination that's the 90 00:09:46.870 --> 00:09:52.580 character 13 and character 10 now UNIX drivers started automatically using the carriage 91 00:09:52.580 --> 00:09:56.420 returns so it wasn't necessary to have it in the file now when you use a backslash 92 00:09:56.420 --> 00:10:00.930 n Pyton takes care of this for you and the same code will work on all three 93 00:10:00.930 --> 00:10:04.459 operating systems in other words it knows by you just putting a /n 94 00:10:04.459 --> 00:10:10.870 then it needs to essentially put a character 13 and a character 10 and therefore on 95 00:10:10.870 --> 00:10:11.740 Windows 96 00:10:11.740 --> 00:10:17.760 and to leave it just a character 10 for Mac and Linux enough of the history well 97 00:10:17.760 --> 00:10:22.500 almost that last bit of history may be an interesting but probably of very 98 00:10:22.500 --> 00:10:25.930 little use but there's a bit more history that is important I'm getting to 99 00:10:25.930 --> 00:10:31.550 that now these raw strings literals that we've used on line 4 their introduced 100 00:10:31.550 --> 00:10:36.130 in Python 2 mainly for using regular expressions now we will be covering regular 101 00:10:36.130 --> 00:10:40.300 expressions later in the course for now all we really need to know about regular 102 00:10:40.300 --> 00:10:44.650 expressions is that the strings that contain a lot of back / characters now 103 00:10:44.650 --> 00:10:49.160 because the backslashes used to escape the next character in a string if you 104 00:10:49.160 --> 00:10:52.840 actually want a backslash then you have to double it up and I'll show you what I mean so 105 00:10:53.410 --> 00:11:01.530 lets continue on here so we're going to type.... 106 00:11:01.530 --> 00:11:15.430 ....and see what happens 107 00:11:17.340 --> 00:11:20.930 notice as what happened their so this is a backslash followed by some texts that's 108 00:11:20.930 --> 00:11:26.210 because the way that we've type that in the / f has been treaded or the f that is after 109 00:11:26.210 --> 00:11:30.880 the / has been treated as a control character and we can't see that on 110 00:11:30.880 --> 00:11:33.830 the screen but the bottom line is the f hasn't been treated as a regular f 111 00:11:33.830 --> 00:11:37.350 in that case so if we want to use a backslash character would have to do 112 00:11:37.350 --> 00:11:39.920 something like this so.... 113 00:11:39.920 --> 00:11:43.890 .... 114 00:11:46.200 --> 00:11:56.720 ....and if we run that 115 00:11:56.720 --> 00:12:00.570 you could see we got it now appearing so / is now being shown because we've 116 00:12:00.570 --> 00:12:04.540 used to backslashes and the f is now showing and incidentally in the previous 117 00:12:04.540 --> 00:12:09.590 example a strange character may appear on Linux and Windows it 118 00:12:09.590 --> 00:12:14.190 doesn't appear as you can see under Mac right here theirs probably a strange 119 00:12:14.190 --> 00:12:17.670 characters so if your seeing that looks a little bit like a or just a 120 00:12:17.670 --> 00:12:23.320 strange character the reason for that is Python interpreting /f as an ASCII 6 121 00:12:23.320 --> 00:12:28.030 which is an echo used by device to acknowledge receiving of a transmission in 122 00:12:28.030 --> 00:12:31.720 the early days so you may or may not see that but the point was that it was 123 00:12:31.720 --> 00:12:35.250 basically not showing a regular f but by putting two backslashes we can see 124 00:12:35.250 --> 00:12:40.120 that its working correctly so again the point of this raw string literals 125 00:12:40.120 --> 00:12:43.700 are introduced to remove the need to double up backslashes in a string like 126 00:12:43.700 --> 00:12:49.410 we've had to do their on line 13 so why now you probably asking why is the 127 00:12:49.410 --> 00:12:52.190 fact that raw stings that were introduced to deal with regular expressions 128 00:12:52.190 --> 00:12:56.230 important well although regular expressions used a lot of backslashes 129 00:12:56.230 --> 00:13:01.130 you can't have one at the end of a regular expressions string now as a result the 130 00:13:01.130 --> 00:13:05.660 Python raw string process it but doesn't suppress escaping if the backslashes the 131 00:13:05.660 --> 00:13:10.950 last character in the string and to show you what I mean I'm going to show by just type in a bit of code we can get it 132 00:13:10.950 --> 00:13:17.150 to show an error that you may not think should be an error so.... 133 00:13:18.760 --> 00:13:29.000 ....so Python treats the final backslashes as escape character 134 00:13:29.000 --> 00:13:33.550 and its escaping the final close quote so consequently the string isn't 135 00:13:33.550 --> 00:13:34.590 terminated 136 00:13:34.590 --> 00:13:39.830 so hence we are getting an error if that makes sense so if you want to end a string with a backslash you have to 137 00:13:39.830 --> 00:13:43.840 escape it even when using raw string literal in other words we have to put a 138 00:13:43.840 --> 00:13:48.020 double backslash in there and that fixes the problem now raw string 139 00:13:48.020 --> 00:13:52.320 literals are sometimes used to specify file paths in Windows which used the 140 00:13:52.320 --> 00:13:55.490 backslash character rather than a forward slash two separate the directory 141 00:13:55.490 --> 00:13:59.750 names but it's not actually necessary to do this because once again Python takes 142 00:13:59.750 --> 00:14:04.190 care of it for us but if you do and your Python ends up with a backslash such a 143 00:14:04.190 --> 00:14:08.470 a c:/ for example then you need to double up that final backslash 144 00:14:08.470 --> 00:14:12.890 so I'll be referring to these raw type literals because there's really no such 145 00:14:12.890 --> 00:14:17.240 thing as a raw string type in Python their all just string which is really 146 00:14:17.240 --> 00:14:21.390 telling Python to alter how it interprets them and that's why I had to 147 00:14:21.390 --> 00:14:25.520 copy and paste the strings rather than somehow convert them into a raw string when 148 00:14:25.520 --> 00:14:29.060 initializing the raw variables there's no such conversion because there's no 149 00:14:29.060 --> 00:14:31.180 raw type to convert to 150 00:14:31.180 --> 00:14:36.290 their are prefix that we're putting you can see on line 16 really only applies to string 151 00:14:36.290 --> 00:14:41.410 literals to tell Python on how to interpret so pep 257 that we saw earlier in 152 00:14:41.410 --> 00:14:45.820 this video was written along time ago and the comment about using a u prefix 153 00:14:45.820 --> 00:14:51.890 doesn't apply to Python 3 if you go back you can read up on prefix all strings in Python 154 00:14:51.890 --> 00:14:56.420 3 are already Unicode that's what I was referring to so I'm going to end the video 155 00:14:56.420 --> 00:15:01.950 here now in the next video we are going to switch back to the doc string and talk more about a how you 156 00:15:01.950 --> 00:15:06.460 got about documenting your classes and methods and functions etc see you in the next 157 00:15:06.460 --> 00:15:06.690 video