1
00:00:05,700 --> 00:00:07,030
alright so there's one more thing that

2
00:00:07,030 --> 00:00:08,420
we need to talk about with these

3
00:00:08,420 --> 00:00:10,730
properties and that's an alternate way

4
00:00:10,730 --> 00:00:12,970
of coding them now in fact this

5
00:00:12,970 --> 00:00:14,660
alternative method I'm going to show you

6
00:00:14,660 --> 00:00:17,480
is more common and you all probably more

7
00:00:17,480 --> 00:00:18,950
likely to see it when you read other

8
00:00:18,950 --> 00:00:21,350
people's python code now it

9
00:00:21,350 --> 00:00:23,930
uses something called decorators and

10
00:00:23,930 --> 00:00:25,520
does exactly the same thing as we've

11
00:00:25,520 --> 00:00:27,980
just seen it's just the syntax that

12
00:00:27,980 --> 00:00:30,530
differs now the decorator results in the

13
00:00:30,530 --> 00:00:32,990
property line being done for you which

14
00:00:32,990 --> 00:00:34,640
is why people tend to prefer it to the

15
00:00:34,640 --> 00:00:36,370
method that we've seen in the previous

16
00:00:36,370 --> 00:00:38,810
video you do need to be aware of both

17
00:00:38,810 --> 00:00:41,150
approaches though and the way we've just

18
00:00:41,150 --> 00:00:43,540
looked at it is easy to understand which

19
00:00:43,540 --> 00:00:45,040
is why we started with that

20
00:00:45,040 --> 00:00:47,080
so let's see how to use this alternative

21
00:00:47,080 --> 00:00:50,330
syntax by adding a property for our score

22
00:00:50,330 --> 00:00:52,090
attribute so we're going to go back to

23
00:00:52,090 --> 00:00:55,240
our player .py class and we're going to

24
00:00:55,240 --> 00:00:59,260
start off by hiding a score variable by

25
00:00:59,260 --> 00:01:02,330
renaming to _score on line 7

26
00:01:02,330 --> 00:01:06,160
right-click the refactor rename will call it

27
00:01:06,160 --> 00:01:10,250
_score you can see it also

28
00:01:10,250 --> 00:01:13,340
change the line I changed the line

29
00:01:13,340 --> 00:01:15,410
25 as well which was also using score

30
00:01:15,410 --> 00:01:19,310
the score data attribute now next thing we

31
00:01:19,310 --> 00:01:21,410
need to do is add the methods to get and

32
00:01:21,410 --> 00:01:24,560
set the score but we decorate those methods

33
00:01:24,560 --> 00:01:26,270
by putting a special comment before them

34
00:01:26,270 --> 00:01:29,030
going to add them after other methods

35
00:01:29,030 --> 00:01:30,260
before our

36
00:01:30,260 --> 00:01:33,590
STR method down here so let's start

37
00:01:33,590 --> 00:01:39,830
by putting....

38
00:01:39,830 --> 00:01:45,380
....

39
00:01:45,380 --> 00:01:48,350
...

40
00:01:48,350 --> 00:01:51,620
...

41
00:01:51,620 --> 00:01:55,040
....

42
00:01:55,040 --> 00:01:57,800
....you can see what happened when we did

43
00:01:57,800 --> 00:02:01,790
that so that's it for the getter we add

44
00:02:01,790 --> 00:02:03,890
the @ property decorator before the

45
00:02:03,890 --> 00:02:05,950
method on the previous line and the

46
00:02:05,950 --> 00:02:08,300
decorator takes care of the score equals

47
00:02:08,300 --> 00:02:10,220
property line that we otherwise

48
00:02:10,220 --> 00:02:13,510
have to write now we haven't covered

49
00:02:13,510 --> 00:02:15,790
decorators yet so just treat this as an

50
00:02:15,790 --> 00:02:18,560
alternative syntax which it really which

51
00:02:18,560 --> 00:02:20,980
is really all that it is now the method

52
00:02:20,980 --> 00:02:23,590
can perform any other tasks it needs to

53
00:02:23,590 --> 00:02:25,060
so could multiply the score by the

54
00:02:25,060 --> 00:02:27,860
number of lives left for example now the

55
00:02:27,860 --> 00:02:29,890
setter is also the same sort method

56
00:02:29,890 --> 00:02:32,690
that was used for the other setters the

57
00:02:32,690 --> 00:02:34,670
decorator this time is in the name of

58
00:02:34,670 --> 00:02:38,000
the property followed by dot setter so

59
00:02:38,000 --> 00:02:40,580
we're going to come down here make a bit

60
00:02:40,580 --> 00:02:41,780
of space here which will clean up a

61
00:02:41,780 --> 00:02:45,860
little bit afterwards we're going to

62
00:02:45,860 --> 00:02:51,530
type....

63
00:02:51,530 --> 00:02:56,590
....

64
00:02:56,590 --> 00:03:00,200
...

65
00:03:00,200 --> 00:03:03,890
...

66
00:03:03,890 --> 00:03:06,890
let's clean this up a little bit now

67
00:03:06,890 --> 00:03:10,810
so again the setter method is the same

68
00:03:10,810 --> 00:03:12,430
sort of method that we've used for the

69
00:03:12,430 --> 00:03:14,330
other setters the difference here is

70
00:03:14,330 --> 00:03:16,420
that the decorator i use this time when

71
00:03:16,420 --> 00:03:18,980
the line prior to it is the name of the

72
00:03:18,980 --> 00:03:21,860
property followed by dot setter you can

73
00:03:21,860 --> 00:03:25,030
see the line 37 now be careful though

74
00:03:25,030 --> 00:03:28,640
on line 39 if you type self dot score

75
00:03:28,640 --> 00:03:31,210
equals score instead of self dot _

76
00:03:31,210 --> 00:03:33,310
equals score you can get the same

77
00:03:33,310 --> 00:03:35,270
recursive problem we had in the previous

78
00:03:35,270 --> 00:03:38,110
video that applies to all the setters

79
00:03:38,110 --> 00:03:39,950
not just because we're creating this one

80
00:03:39,950 --> 00:03:42,430
differently so back in the main

81
00:03:42,430 --> 00:03:45,310
program main.py come down here and

82
00:03:45,310 --> 00:03:49,030
let's go ahead and do...

83
00:03:49,030 --> 00:03:53,960
...we should find that

84
00:03:53,960 --> 00:03:56,210
works run

85
00:03:56,210 --> 00:03:58,150
see that's working fine and we've

86
00:03:58,150 --> 00:03:59,480
got to score incremented or setter

87
00:03:59,480 --> 00:04:03,800
I should say to 500 so that is properties and

88
00:04:03,800 --> 00:04:06,590
getters and setters in python now

89
00:04:06,590 --> 00:04:07,960
there's really nothing to be gained from

90
00:04:07,960 --> 00:04:10,310
making our score into a properly i just

91
00:04:10,310 --> 00:04:11,950
did it to show you this alternative

92
00:04:11,950 --> 00:04:14,840
syntax but getting back to the play.py

93
00:04:14,840 --> 00:04:18,430
while I'm talking but as the

94
00:04:18,430 --> 00:04:20,589
class stares at the moment we could just

95
00:04:20,589 --> 00:04:22,120
have lifted accessing the score data

96
00:04:22,120 --> 00:04:24,430
attribute directly now the setters for

97
00:04:24,430 --> 00:04:26,200
lives and levels do perform useful

98
00:04:26,200 --> 00:04:28,730
functions so changing them to be

99
00:04:28,730 --> 00:04:31,550
properties was a useful thing to do now

100
00:04:31,550 --> 00:04:32,140
the document

101
00:04:32,140 --> 00:04:33,640
for properties can be found at

102
00:04:33,640 --> 00:04:36,100
this link that i'm going to paste into

103
00:04:36,100 --> 00:04:47,910
a browser

104
00:04:47,910 --> 00:04:51,110
you can see that it could be worth

105
00:04:51,110 --> 00:04:52,520
checking that out as well

106
00:04:52,520 --> 00:04:54,390
alright so I'm going to finish this

107
00:04:54,390 --> 00:04:56,100
video here now in the next video we're

108
00:04:56,100 --> 00:04:57,510
going to start looking at inheritance

109
00:04:57,510 --> 00:05:00,300
which is an important and sometimes

110
00:05:00,300 --> 00:05:02,930
overused feature of object-oriented

111
00:05:02,930 --> 00:05:10,180
programming see you in the next video

