WEBVTT 1 00:00:02.260 --> 00:00:06.720 so it's now time to focus on the album classes add song method that we've 2 00:00:06.720 --> 00:00:11.970 delegated the task of dealing with the song too you can see that currently when the 3 00:00:11.970 --> 00:00:16.059 artist class we wanna go to the album class in the file that is obviously this 4 00:00:16.059 --> 00:00:21.130 one starting on line16 so obviously it already exists but we need to make some changes to 5 00:00:21.130 --> 00:00:25.580 get this to work so we are gonna change the arguments a little bit different it's 6 00:00:25.580 --> 00:00:33.430 going to be the title.. 7 00:00:33.430 --> 00:00:38.010 and we need to make some changes here at the moment we've got some very basic validation 8 00:00:38.010 --> 00:00:43.690 here so we need to use the find object function in this code to find the 9 00:00:43.690 --> 00:00:51.159 song if it even exists the track so put... 10 00:00:51.159 --> 00:00:58.839 ...so we are looking for the song tracks if it exists in 11 00:00:58.839 --> 00:01:07.910 the list and if... 12 00:01:07.910 --> 00:01:15.980 ...so we are creating the new object for the song then 13 00:01:15.980 --> 00:01:21.680 below that we gonna put..... 14 00:01:21.680 --> 00:01:32.190 .... 15 00:01:32.190 --> 00:01:36.950 ...so the else is going to be.... 16 00:01:36.950 --> 00:01:44.440 .... 17 00:01:44.440 --> 00:01:52.690 .....so what we doing here well the methods have now change to accept a string for 18 00:01:52.690 --> 00:01:56.710 the song title rather than a sing object after all the method is called by 19 00:01:56.710 --> 00:02:01.780 the artists class which doesn't know about songs and the body of the method is 20 00:02:01.780 --> 00:02:05.120 similar to what it was but this is just that extra step that I added to check if 21 00:02:05.120 --> 00:02:10.639 the songs are already present and then to create a new song object if it isn't and if the song was 22 00:02:10.639 --> 00:02:12.280 already present and no action is taken 23 00:02:12.280 --> 00:02:15.940 now this may or may not be the correct thing to do 24 00:02:15.940 --> 00:02:19.680 bonus tracks on CD is often have the same name as the track in the main album 25 00:02:19.680 --> 00:02:24.330 and in this case it will result in the bonus track not being added so to deal 26 00:02:24.330 --> 00:02:27.870 with this we probably include the track number and the data file and check that 27 00:02:27.870 --> 00:02:32.739 rather than or possibly as well as the title so that's pretty much it the 28 00:02:32.739 --> 00:02:37.480 method uses the same find_object function to check the list and 29 00:02:37.480 --> 00:02:41.319 find_object relies on items in the list some searching for an 30 00:02:41.319 --> 00:02:46.280 attribute called name which you have seen a few times now in the previous videos so 31 00:02:46.280 --> 00:02:49.380 if you run the program now we will probably get an error 32 00:02:49.380 --> 00:02:53.930 and I'll just run it show you and we do get an error 33 00:02:54.750 --> 00:03:01.459 song object has no attribute name and there's a reason for that again find_object relies on 34 00:03:01.459 --> 00:03:06.900 items in the list that its searching having an attribute called name and in this 35 00:03:06.900 --> 00:03:11.600 case the songs title is stored in an attribute called title rather than name but 36 00:03:11.600 --> 00:03:15.790 that turns out to be pretty easy to fix we can just change the self.title to 37 00:03:15.790 --> 00:03:22.620 self.name so come here to line 11 self.title equals title again 38 00:03:22.620 --> 00:03:29.829 initialization for song and will make that self.name instead of title.... 39 00:03:29.829 --> 00:03:34.959 so we are now using names as an attribute for title and we also want to change the create_ 40 00:03:34.959 --> 00:03:38.250 check file function so right down here 41 00:03:38.250 --> 00:03:44.690 on line 141 so at the moment that's got title their we want to change the title to be 42 00:03:44.690 --> 00:03:50.489 name as well so of course thats printing out the title attribute from 43 00:03:50.489 --> 00:03:56.690 song object but we're now changing that to use the name attribute because we renamed 44 00:03:56.690 --> 00:03:58.430 the attribute 45 00:03:58.430 --> 00:04:08.390 and we will just delete the checkfile.txt....and lets run this again 46 00:04:09.450 --> 00:04:16.530 and we can see its now processing as we expected to do and we can see theirs 28 artist 47 00:04:16.530 --> 00:04:20.620 now because the program hasn't been released and nobody's relying on the 48 00:04:20.620 --> 00:04:24.240 song class in the code changing the attribute in this way that we've done in 49 00:04:24.240 --> 00:04:29.010 this video is fine but if we hadn't released the program and their was a chance 50 00:04:29.010 --> 00:04:33.250 that other people could be importing it you know to use our classes in changing 51 00:04:33.250 --> 00:04:36.440 attribute wouldn't be a good idea and would have to 52 00:04:36.440 --> 00:04:40.580 take another approach instead but I'll come back to an alternative approach to 53 00:04:40.580 --> 00:04:44.650 that in a minute but the program is now taking an object oriented approach and 54 00:04:44.650 --> 00:04:50.350 as a result it's a lot easier to understand it's also easy to 55 00:04:50.350 --> 00:04:55.889 modify if necessary and theirs less chance of bugs creeping in when we do so simpler code is 56 00:04:55.889 --> 00:04:59.979 generally more reliable and by delegating tasks to other objects we've 57 00:04:59.979 --> 00:05:04.180 kept each step much simpler and more focused on a single task rather than 58 00:05:04.180 --> 00:05:08.790 having that huge load_data method that we did previously so hopefully seeing how this 59 00:05:08.790 --> 00:05:09.979 program is changed 60 00:05:09.979 --> 00:05:14.090 was trying change to use object oriented techniques help to demonstrate some of 61 00:05:14.090 --> 00:05:18.200 the advantages that object oriented programming can provide now it's not 62 00:05:18.200 --> 00:05:21.919 essential to use object oriented techniques and sometimes an extra 63 00:05:21.919 --> 00:05:26.729 overhead that goes with it can impact performance and a functional imperative 64 00:05:26.729 --> 00:05:31.120 approach might be more suitable but when used correctly though object oriented 65 00:05:31.120 --> 00:05:34.690 programming can result in simpler code fewer errors and ultimately 66 00:05:34.690 --> 00:05:39.400 faster development now before I finish with this example for now anyway 67 00:05:39.400 --> 00:05:44.500 we are gonna removed the secular references from our objects and also make this program work 68 00:05:44.500 --> 00:05:48.970 without renaming the song classes title attribute or at least show 69 00:05:48.970 --> 00:05:53.539 a way to allow code that used title to still work even though our code expects the 70 00:05:53.539 --> 00:05:58.979 attribute to be called name so first we need to do is go back close this window 71 00:05:58.979 --> 00:06:03.419 down go back to line 11 again and we're gonna change the self.name back 72 00:06:03.419 --> 00:06:08.630 to title so we can see that alternative approach back to title again 73 00:06:08.630 --> 00:06:12.590 and obviously the program will crash and we go down and change checkfile 74 00:06:12.590 --> 00:06:15.610 again as well change this back to title as well 75 00:06:16.889 --> 00:06:20.770 and by doing that the program is back in the state where it wouldn't work because 76 00:06:20.770 --> 00:06:25.199 the find object functions expecting its objects to have a name field or 77 00:06:25.199 --> 00:06:29.569 an attribute which if won't have of course because we just changed name back to title now because 78 00:06:29.569 --> 00:06:33.479 we want to run the program after removing the circular references we have 79 00:06:33.479 --> 00:06:38.930 to fix the problem with the title attribute first so that it can compile at least so I'm gonna explain this 80 00:06:38.930 --> 00:06:44.069 next step in more detail on a later video but for now I just want to show that this is 81 00:06:44.069 --> 00:06:47.969 possible now also anyone who's experienced in object-oriented 82 00:06:47.969 --> 00:06:52.639 programming might be concerned that our classes are allowing direct access to 83 00:06:52.639 --> 00:06:56.379 the attributes now this isn't necessarily a bad thing 84 00:06:56.379 --> 00:06:59.830 remembering that Python does things differently but this is also good 85 00:06:59.830 --> 00:07:04.289 opportunity to show you that you can allow access via getters and setters and 86 00:07:04.289 --> 00:07:08.050 you can even restrict access so that getters and setters are the only way to retrieve 87 00:07:08.050 --> 00:07:15.439 and change attributes so to do that we need to go back up to our song class and 88 00:07:15.439 --> 00:07:21.110 come down here we need to introduce a new method and put... 89 00:07:21.110 --> 00:07:30.710 .... 90 00:07:30.710 --> 00:07:33.710 ... 91 00:07:35.439 --> 00:07:43.129 .... 92 00:07:43.630 --> 00:07:48.330 so as I mentioned the get_title method is a getter for those people who are 93 00:07:48.330 --> 00:07:52.610 used to object oriented programming and if this is your first experience of 94 00:07:52.610 --> 00:07:55.500 object oriented programming then don't worry if you don't know what a getter 95 00:07:55.500 --> 00:08:00.790 is because I'm going to be explaining all that later but below that on line 18 I 96 00:08:00.790 --> 00:08:05.160 defined a read only property called name that uses the getter to return the 97 00:08:05.160 --> 00:08:10.070 value of the title attribute so in this way existing code such as the create_check 98 00:08:10.070 --> 00:08:14.870 file function will still work but we've then be able to use the find 99 00:08:14.870 --> 00:08:21.730 object function as well so if we run this bearing in mind that last time we run this with the self.title 100 00:08:21.730 --> 00:08:22.720 changes in here 101 00:08:22.720 --> 00:08:25.810 the original code it crashed because It couldn't find the name attribute 102 00:08:26.690 --> 00:08:35.370 but now we've added this on line 18 and I'll add another line to keep IntelliJ happy if we run this now we see that 103 00:08:35.370 --> 00:08:39.580 the code now does work nicely even though there isn't technically a name 104 00:08:39.580 --> 00:08:44.980 attribute in our song class so what we've done is we've added a property called name to 105 00:08:44.980 --> 00:08:48.870 the song class to allow it to be used without find_object function 106 00:08:48.870 --> 00:08:55.230 and we've done so without breaking any existing code so you can see that that's another alternative 107 00:08:55.230 --> 00:08:58.910 that would be very important if your code had been released and being used by 108 00:08:58.910 --> 00:09:03.320 other people and as I said I'm going to be covering getters and setters in this method of defining 109 00:09:03.320 --> 00:09:07.410 properties in later video and when I do that you'll also see why I didn't just set 110 00:09:07.410 --> 00:09:11.760 self.name = title in the init method so in other words I didn't do something 111 00:09:11.760 --> 00:09:19.440 like this..so when I do show you that in a video I'm gonna demonstrate why 112 00:09:19.440 --> 00:09:26.520 I didn't do that do that approach and why I used property instead so remove that for now so 113 00:09:26.520 --> 00:09:33.110 we are back to the fact that we've got a working program so the next video we 114 00:09:33.110 --> 00:09:36.200 going to go through remove the circular references and then we'll move on to a 115 00:09:36.200 --> 00:09:38.090 challenge so see in the next video