WEBVTT 1 00:00:01.920 --> 00:00:06.509 so once again with now got a working program but it is now time to remove 2 00:00:06.509 --> 00:00:11.309 those circular references that we've talked about previously now there is no 3 00:00:11.309 --> 00:00:15.700 need for the song class to store an artist object anymore we now have a way 4 00:00:15.700 --> 00:00:20.990 to find an artist in the list if we know the artist's name and that's using the find_ 5 00:00:20.990 --> 00:00:26.810 object function which you've seen as using the code and in addition theirs no need for the 6 00:00:26.810 --> 00:00:30.680 album class to store the artist object once again the name is all that we need 7 00:00:30.680 --> 00:00:32.149 to be able to 8 00:00:32.149 --> 00:00:36.760 reference that again using the find_object function so it makes 9 00:00:36.760 --> 00:00:41.289 things a lot easier and the class diagram is now much simplified and I just want to bring 10 00:00:41.289 --> 00:00:46.739 that up on screen you can see there that we can simply this a lot with it is 11 00:00:46.739 --> 00:00:49.479 simplified by removing that 12 00:00:49.479 --> 00:00:53.420 relationship so once we do that we'll end up with something like this 13 00:00:53.420 --> 00:00:57.879 the top diagram as you can see there so that's what we're trying to achieve so 14 00:00:57.879 --> 00:01:03.119 how do we go about doing that and removing the circular references 15 00:01:03.119 --> 00:01:07.780 well thats the challenge this is the challenge that I'm gonna set you to actually fix in this 16 00:01:07.780 --> 00:01:16.060 video so let me just paste in the text for the challenge so you know what is exactly needs to be done ok so here is what we 17 00:01:16.060 --> 00:01:20.850 want to do we want to modify the program so that the class structure matches the 18 00:01:20.850 --> 00:01:27.000 simplified diagram now artist objects can hold references to album objects and album 19 00:01:27.000 --> 00:01:30.850 objects can hold references to song objects but the key here is there must 20 00:01:30.850 --> 00:01:34.780 be no circular references so that is really what you're trying to achieve here is to 21 00:01:34.780 --> 00:01:39.740 remove that or set up so their aren't circular references so its now time to do that 22 00:01:39.740 --> 00:01:43.189 so pause the video and go away and see if you can complete that challenge when you 23 00:01:43.189 --> 00:01:46.170 ready to come back and check our solution start the video again and I'll 24 00:01:46.170 --> 00:01:49.170 go through and show you how to do that 25 00:01:51.650 --> 00:01:57.300 ok so hopefully you managed to solve it so let's go through and see if we can get this working 26 00:01:57.300 --> 00:02:03.010 so I'm just going to undo these changes and removed this from the doc 27 00:02:03.010 --> 00:02:10.950 strings because we don't need it their so what need to do well the first change is in the song class and what we gonna do is first change 28 00:02:10.950 --> 00:02:14.960 the doc string to reflect the fact that the song subject is now going to store the 29 00:02:14.960 --> 00:02:19.240 name of the artists rather than a reference to the artist object so 30 00:02:19.240 --> 00:02:23.770 come up to the class song which is on the screen at the moment 31 00:02:23.770 --> 00:02:29.550 we got artist which is an artist we are going to change it because it is going to be a string now and it's 32 00:02:29.550 --> 00:02:37.020 going to be the name of the songs creator.... 33 00:02:37.020 --> 00:02:42.050 and we don't need to make any other changes its pretty straightforward 34 00:02:42.050 --> 00:02:46.300 that's really all we need to do with the song class but of course when we create 35 00:02:46.300 --> 00:02:49.500 new song objects were gonna be passing the artists name as a string to the init 36 00:02:49.500 --> 00:02:53.480 method instead of passing the artist object and we are doing this to remove 37 00:02:53.480 --> 00:02:57.790 circular references so the next step is to go ahead and do that to remove the 38 00:02:57.790 --> 00:03:01.060 circular reference between album and artist 39 00:03:01.060 --> 00:03:06.370 so go to class album and bring it on the screen here so artists the artists responsible 40 00:03:06.370 --> 00:03:08.019 for the album if not specified 41 00:03:08.019 --> 00:03:17.239 this is now going to be a string here that's the name of the artist name of the 42 00:03:17.239 --> 00:03:20.410 artist as supposed to the object responsible for the album and if not specified 43 00:03:20.980 --> 00:03:24.380 that's all exactly the same as it what it was before 44 00:03:24.380 --> 00:03:31.690 down here on line 39 we got self.artist equals various artist if the parameter 45 00:03:31.690 --> 00:03:34.670 pass or the argument pass was none we are going to change that 46 00:03:34.670 --> 00:03:38.900 so you no longer going to be trying to create an artist object we are going to be setting that to string 47 00:03:38.900 --> 00:03:43.940 the string of various artists that's all the change we need here now the 48 00:03:43.940 --> 00:03:48.370 add song method doesn't need to be changed the reason for that is that it uses 49 00:03:48.370 --> 00:03:53.120 self.artist and then now we are storing artist as a string that will automatically 50 00:03:53.120 --> 00:03:58.600 be used when creating the songs so finally we need to change the artists class so 51 00:03:58.600 --> 00:04:03.130 that when it creates a new album it's using its name rather than passing self 52 00:04:03.130 --> 00:04:07.330 so in that way the artist class will only be storing the artist name as a string rather 53 00:04:07.330 --> 00:04:11.580 than storing a reference to the artist object as it is currently doing so 54 00:04:11.580 --> 00:04:14.020 scrolling down to look at the artist class 55 00:04:14.020 --> 00:04:19.110 the change will make the album we 56 00:04:19.110 --> 00:04:22.410 got equals new album name year and self 57 00:04:22.410 --> 00:04:29.280 we no longer want to pass self we want to pass the attribute self.name so that is 58 00:04:29.280 --> 00:04:33.150 literally the only things that we should need to change at this point the program 59 00:04:33.150 --> 00:04:38.060 should know run and will tests that shortly so the artists class stores references 60 00:04:38.060 --> 00:04:42.510 to albums in its list now and the album class towards references to songs but 61 00:04:42.510 --> 00:04:46.390 they aren't circular references any longer so neither the song class nor the album 62 00:04:46.390 --> 00:04:50.240 class store artist objects anymore they just hold the name of the artist as a 63 00:04:50.240 --> 00:04:54.820 string and of course they get looked up with the find_object function when we 64 00:04:54.820 --> 00:04:58.990 need to find out more information about that so lets run this and confirm that 65 00:04:58.990 --> 00:05:10.220 it does run...so we will run this and we still get the same results as we got before so everything is 66 00:05:10.220 --> 00:05:14.060 working nicely with the changes that we're actually made so that's actually it 67 00:05:14.060 --> 00:05:18.820 that is a basic introduction to object-oriented programming in Python and 68 00:05:18.820 --> 00:05:24.470 we've seen what classes are now and how to create our own basic classes and 69 00:05:24.470 --> 00:05:29.510 we also look at the concepts of encapsulation and delegation now by taking a program and 70 00:05:29.510 --> 00:05:34.060 changing it to use object oriented techniques hopefully we've given you an 71 00:05:34.060 --> 00:05:38.460 appreciation of how this alternative approach can really provide huge benefit 72 00:05:38.460 --> 00:05:43.430 and reliability and also allow you to become more productive at the same time now we 73 00:05:43.430 --> 00:05:47.710 have really only scratched the surface of object oriented programming is really 74 00:05:47.710 --> 00:05:52.260 a lot more to cover yet and in Python their is also different implementation options to 75 00:05:52.260 --> 00:05:56.230 consider so I'm going to be covering all that in the remaining videos in the section so 76 00:05:56.230 --> 00:05:57.250 I'll see you in the next one