WEBVTT 1 00:00:01.760 --> 00:00:03.739 alright so there's one more thing that 2 00:00:03.739 --> 00:00:05.120 we need to talk about with these alright so there's one more thing that 3 00:00:05.120 --> 00:00:07.430 properties and that's an alternate way we need to talk about with these 4 00:00:07.430 --> 00:00:09.679 of coding them now in fact this properties and that's an alternate way 5 00:00:09.679 --> 00:00:11.360 alternative method I'm going to show you of coding them now in fact this 6 00:00:11.360 --> 00:00:14.180 is more common and you all probably more alternative method I'm going to show you 7 00:00:14.180 --> 00:00:15.650 likely to see it when you read other is more common and you all probably more 8 00:00:15.650 --> 00:00:18.050 people's python code now it likely to see it when you read other 9 00:00:18.050 --> 00:00:20.630 uses something called decorators and people's python code now it 10 00:00:20.630 --> 00:00:22.220 does exactly the same thing as we've uses something called decorators and 11 00:00:22.220 --> 00:00:24.680 just seen it's just the syntax that does exactly the same thing as we've 12 00:00:24.680 --> 00:00:27.230 differs now the decorator results in the just seen it's just the syntax that 13 00:00:27.230 --> 00:00:29.690 property line being done for you which differs now the decorator results in the 14 00:00:29.690 --> 00:00:31.340 is why people tend to prefer it to the property line being done for you which 15 00:00:31.340 --> 00:00:33.079 method that we've seen in the previous is why people tend to prefer it to the 16 00:00:33.079 --> 00:00:35.510 video you do need to be aware of both method that we've seen in the previous 17 00:00:35.510 --> 00:00:37.850 approaches though and the way we've just video you do need to be aware of both 18 00:00:37.850 --> 00:00:40.249 looked at it is easy to understand which approaches though and the way we've just 19 00:00:40.249 --> 00:00:41.749 is why we started with that looked at it is easy to understand which 20 00:00:41.749 --> 00:00:43.789 so let's see how to use this alternative is why we started with that 21 00:00:43.789 --> 00:00:47.030 syntax by adding a property for our score so let's see how to use this alternative 22 00:00:47.030 --> 00:00:48.799 attribute so we're going to go back to syntax by adding a property for our score 23 00:00:48.799 --> 00:00:51.949 our player .py class and we're going to attribute so we're going to go back to 24 00:00:51.949 --> 00:00:55.969 start off by hiding a score variable by our player .py class and we're going to 25 00:00:55.969 --> 00:00:59.030 renaming to _score on line 7 start off by hiding a score variable by 26 00:00:59.030 --> 00:01:02.869 right-click the refactor rename will call it renaming to _score on line 7 27 00:01:02.869 --> 00:01:06.950 _score you can see it also right-click the refactor rename will call it 28 00:01:06.950 --> 00:01:10.040 change the line I changed the line _score you can see it also 29 00:01:10.040 --> 00:01:12.110 25 as well which was also using score change the line I changed the line 30 00:01:12.110 --> 00:01:16.010 the score data attribute now next thing we 25 as well which was also using score 31 00:01:16.010 --> 00:01:18.110 need to do is add the methods to get and the score data attribute now next thing we 32 00:01:18.110 --> 00:01:21.260 set the score but we decorate those methods need to do is add the methods to get and 33 00:01:21.260 --> 00:01:22.970 by putting a special comment before them set the score but we decorate those methods 34 00:01:22.970 --> 00:01:25.730 going to add them after other methods by putting a special comment before them 35 00:01:25.730 --> 00:01:26.960 before our going to add them after other methods 36 00:01:26.960 --> 00:01:30.290 STR method down here so let's start before our 37 00:01:30.290 --> 00:01:36.530 by putting.... STR method down here so let's start 38 00:01:36.530 --> 00:01:42.080 .... by putting.... 39 00:01:42.080 --> 00:01:45.050 ... .... 40 00:01:45.050 --> 00:01:48.320 ... ... 41 00:01:48.320 --> 00:01:51.740 .... ... 42 00:01:51.740 --> 00:01:54.500 ....you can see what happened when we did .... 43 00:01:54.500 --> 00:01:58.490 that so that's it for the getter we add ....you can see what happened when we did 44 00:01:58.490 --> 00:02:00.590 the @ property decorator before the that so that's it for the getter we add 45 00:02:00.590 --> 00:02:02.659 method on the previous line and the the @ property decorator before the 46 00:02:02.659 --> 00:02:05.000 decorator takes care of the score equals method on the previous line and the 47 00:02:05.000 --> 00:02:06.920 property line that we otherwise decorator takes care of the score equals 48 00:02:06.920 --> 00:02:10.219 have to write now we haven't covered property line that we otherwise 49 00:02:10.219 --> 00:02:12.499 decorators yet so just treat this as an have to write now we haven't covered 50 00:02:12.499 --> 00:02:15.260 alternative syntax which it really which decorators yet so just treat this as an 51 00:02:15.260 --> 00:02:17.689 is really all that it is now the method alternative syntax which it really which 52 00:02:17.689 --> 00:02:20.299 can perform any other tasks it needs to is really all that it is now the method 53 00:02:20.299 --> 00:02:21.769 so could multiply the score by the can perform any other tasks it needs to 54 00:02:21.769 --> 00:02:24.560 number of lives left for example now the so could multiply the score by the 55 00:02:24.560 --> 00:02:26.599 setter is also the same sort method number of lives left for example now the 56 00:02:26.599 --> 00:02:29.390 that was used for the other setters the setter is also the same sort method 57 00:02:29.390 --> 00:02:31.370 decorator this time is in the name of that was used for the other setters the 58 00:02:31.370 --> 00:02:34.700 the property followed by dot setter so decorator this time is in the name of 59 00:02:34.700 --> 00:02:37.280 we're going to come down here make a bit the property followed by dot setter so 60 00:02:37.280 --> 00:02:38.480 of space here which will clean up a we're going to come down here make a bit 61 00:02:38.480 --> 00:02:42.560 little bit afterwards we're going to of space here which will clean up a 62 00:02:42.560 --> 00:02:48.230 type.... little bit afterwards we're going to 63 00:02:48.230 --> 00:02:53.299 .... type.... 64 00:02:53.299 --> 00:02:56.900 ... .... 65 00:02:56.900 --> 00:03:00.590 ... ... 66 00:03:00.590 --> 00:03:03.590 let's clean this up a little bit now ... 67 00:03:03.590 --> 00:03:07.519 so again the setter method is the same let's clean this up a little bit now 68 00:03:07.519 --> 00:03:09.139 sort of method that we've used for the so again the setter method is the same 69 00:03:09.139 --> 00:03:11.030 other setters the difference here is sort of method that we've used for the 70 00:03:11.030 --> 00:03:13.129 that the decorator i use this time when other setters the difference here is 71 00:03:13.129 --> 00:03:15.680 the line prior to it is the name of the that the decorator i use this time when 72 00:03:15.680 --> 00:03:18.560 property followed by dot setter you can the line prior to it is the name of the 73 00:03:18.560 --> 00:03:21.739 see the line 37 now be careful though property followed by dot setter you can 74 00:03:21.739 --> 00:03:25.340 on line 39 if you type self dot score see the line 37 now be careful though 75 00:03:25.340 --> 00:03:27.919 equals score instead of self dot _ on line 39 if you type self dot score 76 00:03:27.919 --> 00:03:30.019 equals score you can get the same equals score instead of self dot _ 77 00:03:30.019 --> 00:03:31.970 recursive problem we had in the previous equals score you can get the same 78 00:03:31.970 --> 00:03:34.819 video that applies to all the setters recursive problem we had in the previous 79 00:03:34.819 --> 00:03:36.650 not just because we're creating this one video that applies to all the setters 80 00:03:36.650 --> 00:03:39.139 differently so back in the main not just because we're creating this one 81 00:03:39.139 --> 00:03:42.019 program main.py come down here and differently so back in the main 82 00:03:42.019 --> 00:03:45.739 let's go ahead and do... program main.py come down here and 83 00:03:45.739 --> 00:03:50.660 ...we should find that let's go ahead and do... 84 00:03:50.660 --> 00:03:52.910 works run ...we should find that 85 00:03:52.910 --> 00:03:54.859 see that's working fine and we've works run 86 00:03:54.859 --> 00:03:56.180 got to score incremented or setter see that's working fine and we've 87 00:03:56.180 --> 00:04:00.500 I should say to 500 so that is properties and got to score incremented or setter 88 00:04:00.500 --> 00:04:03.290 getters and setters in python now I should say to 500 so that is properties and 89 00:04:03.290 --> 00:04:04.669 there's really nothing to be gained from getters and setters in python now 90 00:04:04.669 --> 00:04:07.010 making our score into a properly i just there's really nothing to be gained from 91 00:04:07.010 --> 00:04:08.659 did it to show you this alternative making our score into a properly i just 92 00:04:08.659 --> 00:04:11.540 syntax but getting back to the play.py did it to show you this alternative 93 00:04:11.540 --> 00:04:15.139 while I'm talking but as the syntax but getting back to the play.py 94 00:04:15.139 --> 00:04:17.299 class stares at the moment we could just while I'm talking but as the 95 00:04:17.299 --> 00:04:18.829 have lifted accessing the score data class stares at the moment we could just 96 00:04:18.829 --> 00:04:21.139 attribute directly now the setters for have lifted accessing the score data 97 00:04:21.139 --> 00:04:22.909 lives and levels do perform useful attribute directly now the setters for 98 00:04:22.909 --> 00:04:25.430 functions sso changing them to be lives and levels do perform useful 99 00:04:25.430 --> 00:04:28.250 properties was a useful thing to do now functions sso changing them to be 100 00:04:28.250 --> 00:04:28.849 the document properties was a useful thing to do now 101 00:04:28.849 --> 00:04:30.349 for properties can be found at the document 102 00:04:30.349 --> 00:04:32.809 this link that i'm going to paste into for properties can be found at 103 00:04:32.809 --> 00:04:44.610 a browser this link that i'm going to paste into 104 00:04:44.610 --> 00:04:47.819 you can see that it could be worth a browser 105 00:04:47.819 --> 00:04:49.229 checking that out as well you can see that it could be worth 106 00:04:49.229 --> 00:04:51.090 alright so I'm going to finish this checking that out as well 107 00:04:51.090 --> 00:04:52.800 video here now in the next video we're alright so I'm going to finish this 108 00:04:52.800 --> 00:04:54.210 going to start looking at inheritance video here now in the next video we're 109 00:04:54.210 --> 00:04:57.000 which is an important and sometimes going to start looking at inheritance 110 00:04:57.000 --> 00:04:59.639 overused feature of object-oriented which is an important and sometimes 111 00:04:59.639 --> 00:05:06.889 programming see you in the next video overused feature of object-oriented