1
00:00:05,780 --> 00:00:10,240
so it's now time to focus on the album
classes add song method that we've

2
00:00:10,240 --> 00:00:15,490
delegated the task of dealing with the
song too you can see that currently when the

3
00:00:15,490 --> 00:00:19,570
artist class we wanna go to the album
class in the file that is obviously this

4
00:00:19,570 --> 00:00:24,650
one starting on line16 so obviously it already
exists but we need to make some changes to

5
00:00:24,650 --> 00:00:29,100
get this to work so we are gonna change the
arguments a little bit different it's

6
00:00:29,100 --> 00:00:36,950
going to be the title..

7
00:00:36,950 --> 00:00:41,530
and we need to make some changes here at the moment
we've got some very basic validation

8
00:00:41,530 --> 00:00:47,210
here so we need to use the find object
function in this code to find the

9
00:00:47,210 --> 00:00:54,670
song if it even exists the track so put...

10
00:00:54,670 --> 00:01:02,350
...so we are looking for the song tracks if it exists in

11
00:01:02,350 --> 00:01:11,430
the list and if...

12
00:01:11,430 --> 00:01:19,500
...so we are creating the new object for the song then

13
00:01:19,500 --> 00:01:25,200
below that we gonna put.....

14
00:01:25,200 --> 00:01:35,710
....

15
00:01:35,710 --> 00:01:40,470
...so the else is going to be....

16
00:01:40,470 --> 00:01:47,960
....

17
00:01:47,960 --> 00:01:56,210
.....so what we doing here well the methods have now change to accept a string for

18
00:01:56,210 --> 00:02:00,230
the song title rather than a sing
object after all the method is called by

19
00:02:00,230 --> 00:02:05,300
the artists class which doesn't know
about songs and the body of the method is

20
00:02:05,300 --> 00:02:08,639
similar to what it was but this is just
that extra step that I added to check if

21
00:02:08,639 --> 00:02:14,150
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:14,150 --> 00:02:15,800
already present and no action is taken

23
00:02:15,800 --> 00:02:19,460
now this may or may not be the correct
thing to do

24
00:02:19,460 --> 00:02:23,200
bonus tracks on CD is often have the
same name as the track in the main album

25
00:02:23,200 --> 00:02:27,850
and in this case it will result in the
bonus track not being added so to deal

26
00:02:27,850 --> 00:02:31,390
with this we probably include the track
number and the data file and check that

27
00:02:31,390 --> 00:02:36,250
rather than or possibly as well as
the title so that's pretty much it the

28
00:02:36,250 --> 00:02:41,000
method uses the same find_object function to check the list and

29
00:02:41,000 --> 00:02:44,830
find_object relies on items in
the list some searching for an

30
00:02:44,830 --> 00:02:49,800
attribute called name which you have seen a
few times now in the previous videos so

31
00:02:49,800 --> 00:02:52,900
if you run the program now we will probably get an error

32
00:02:52,900 --> 00:02:57,450
and I'll just run it show you and we do get an error

33
00:02:58,270 --> 00:03:04,970
song object has no attribute name and
there's a reason for that again find_object relies on

34
00:03:04,970 --> 00:03:10,420
items in the list that its searching
having an attribute called name and in this

35
00:03:10,420 --> 00:03:15,120
case the songs title is stored in an attribute called title rather than name but

36
00:03:15,120 --> 00:03:19,310
that turns out to be pretty easy to fix
we can just change the self.title to

37
00:03:19,310 --> 00:03:26,140
self.name so come here to line 11 self.title equals title again

38
00:03:26,140 --> 00:03:33,340
initialization for song and will make that self.name instead of title....

39
00:03:33,340 --> 00:03:38,470
so we are now using names as an attribute for title and we also want to change the create_

40
00:03:38,470 --> 00:03:41,770
check file function so right down
here

41
00:03:41,770 --> 00:03:48,210
on line 141 so at the moment that's got title their we want to change the title to be

42
00:03:48,210 --> 00:03:54,000
name as well so of course thats printing out the title attribute from

43
00:03:54,000 --> 00:04:00,210
song object but we're now
changing that to use the name attribute because we renamed

44
00:04:00,210 --> 00:04:01,950
the attribute

45
00:04:01,950 --> 00:04:11,910
and we will just delete the checkfile.txt....and lets run this again

46
00:04:12,970 --> 00:04:20,050
and we can see its now processing as we expected to do and we can see theirs 28 artist

47
00:04:20,050 --> 00:04:24,140
now because the program hasn't been
released and nobody's relying on the

48
00:04:24,140 --> 00:04:27,760
song class in the code changing the
attribute in this way that we've done in

49
00:04:27,760 --> 00:04:32,530
this video is fine but if we hadn't
released the program and their was a chance

50
00:04:32,530 --> 00:04:36,770
that other people could be importing it
you know to use our classes in changing

51
00:04:36,770 --> 00:04:39,960
attribute wouldn't be a good idea and
would have to

52
00:04:39,960 --> 00:04:44,100
take another approach instead but I'll
come back to an alternative approach to

53
00:04:44,100 --> 00:04:48,170
that in a minute but the program is now
taking an object oriented approach and

54
00:04:48,170 --> 00:04:53,870
as a result it's a lot
easier to understand it's also easy to

55
00:04:53,870 --> 00:04:59,400
modify if necessary and theirs less chance of bugs creeping in when we do so simpler code is

56
00:04:59,400 --> 00:05:03,490
generally more reliable and by
delegating tasks to other objects we've

57
00:05:03,490 --> 00:05:07,700
kept each step much simpler and more
focused on a single task rather than

58
00:05:07,700 --> 00:05:12,310
having that huge load_data method that we did previously so hopefully seeing how this

59
00:05:12,310 --> 00:05:13,490
program is changed

60
00:05:13,490 --> 00:05:17,610
was trying change to use object oriented
techniques help to demonstrate some of

61
00:05:17,610 --> 00:05:21,720
the advantages that object oriented
programming can provide now it's not

62
00:05:21,720 --> 00:05:25,430
essential to use object oriented
techniques and sometimes an extra

63
00:05:25,430 --> 00:05:30,240
overhead that goes with it can impact
performance and a functional imperative

64
00:05:30,240 --> 00:05:34,640
approach might be more suitable but when
used correctly though object oriented

65
00:05:34,640 --> 00:05:38,210
programming can result in simpler code fewer errors and ultimately

66
00:05:38,210 --> 00:05:42,920
faster development now before I finish with this example for now anyway

67
00:05:42,920 --> 00:05:48,020
we are gonna removed the secular references from our
objects and also make this program work

68
00:05:48,020 --> 00:05:52,490
without renaming the song classes
title attribute or at least show

69
00:05:52,490 --> 00:05:57,050
a way to allow code that used title to still work even though our code expects the

70
00:05:57,050 --> 00:06:02,490
attribute to be called name so first we
need to do is go back close this window

71
00:06:02,490 --> 00:06:06,930
down go back to line 11 again and
we're gonna change the self.name back

72
00:06:06,930 --> 00:06:12,150
to title so we can see that
alternative approach back to title again

73
00:06:12,150 --> 00:06:16,110
and obviously the program will crash and we go down and change checkfile

74
00:06:16,110 --> 00:06:19,130
again as well change this back to title as
well

75
00:06:20,400 --> 00:06:24,290
and by doing that the program is back in
the state where it wouldn't work because

76
00:06:24,290 --> 00:06:28,710
the find object functions expecting its
objects to have a name field or

77
00:06:28,710 --> 00:06:33,080
an attribute which if won't have of course because we just
changed name back to title now because

78
00:06:33,080 --> 00:06:36,990
we want to run the program after
removing the circular references we have

79
00:06:36,990 --> 00:06:42,450
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:42,450 --> 00:06:47,580
next step in more detail on a later video but
for now I just want to show that this is

81
00:06:47,580 --> 00:06:51,480
possible now also anyone who's
experienced in object-oriented

82
00:06:51,480 --> 00:06:56,150
programming might be concerned that our
classes are allowing direct access to

83
00:06:56,150 --> 00:06:59,890
the attributes now this isn't
necessarily a bad thing

84
00:06:59,890 --> 00:07:03,350
remembering that Python does things
differently but this is also good

85
00:07:03,350 --> 00:07:07,800
opportunity to show you that you can
allow access via getters and setters and

86
00:07:07,800 --> 00:07:11,570
you can even restrict access so that
getters and setters are the only way to retrieve

87
00:07:11,570 --> 00:07:18,950
and change attributes so to do that we need
to go back up to our song class and

88
00:07:18,950 --> 00:07:24,630
come down here we need to introduce a
new method and put...

89
00:07:24,630 --> 00:07:34,230
....

90
00:07:34,230 --> 00:07:37,230
...

91
00:07:38,950 --> 00:07:46,640
....

92
00:07:47,150 --> 00:07:51,850
so as I mentioned the get_title method is a getter for those people who are

93
00:07:51,850 --> 00:07:56,130
used to object oriented programming and
if this is your first experience of

94
00:07:56,130 --> 00:07:59,020
object oriented programming then don't
worry if you don't know what a getter

95
00:07:59,020 --> 00:08:04,310
is because I'm going to be explaining all that
later but below that on line 18 I

96
00:08:04,310 --> 00:08:08,680
defined a read only property called
name that uses the getter to return the

97
00:08:08,680 --> 00:08:13,590
value of the title attribute so in this way
existing code such as the create_check

98
00:08:13,590 --> 00:08:18,390
file function will still work but
we've then be able to use the find

99
00:08:18,390 --> 00:08:25,250
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:25,250 --> 00:08:26,240
changes in here

101
00:08:26,240 --> 00:08:29,330
the original code it crashed because It
couldn't find the name attribute

102
00:08:30,210 --> 00:08:38,890
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:38,890 --> 00:08:43,100
the code now does work nicely even
though there isn't technically a name

104
00:08:43,100 --> 00:08:48,500
attribute in our song class so what we've done is
we've added a property called name to

105
00:08:48,500 --> 00:08:52,390
the song class to allow it to be used
without find_object function

106
00:08:52,390 --> 00:08:58,750
and we've done so without breaking any existing code so you can see that that's another alternative

107
00:08:58,750 --> 00:09:02,430
that would be very important if your
code had been released and being used by

108
00:09:02,430 --> 00:09:06,840
other people and as I said I'm going to be covering getters and setters in this method of defining

109
00:09:06,840 --> 00:09:10,930
properties in later video and when I do
that you'll also see why I didn't just set

110
00:09:10,930 --> 00:09:15,280
self.name = title in the init method
so in other words I didn't do something

111
00:09:15,280 --> 00:09:22,960
like this..so when I do show
you that in a video I'm gonna demonstrate why

112
00:09:22,960 --> 00:09:30,040
I didn't do that do that approach and why I used property instead so remove that for now so

113
00:09:30,040 --> 00:09:36,630
we are back to the fact that we've got
a working program so the next video we

114
00:09:36,630 --> 00:09:39,720
going to go through remove the circular
references and then we'll move on to a

115
00:09:39,720 --> 00:09:41,610
challenge so see in the next video

