Access Control Continued
00:00 Welcome to lesson six in Object-Oriented Programming in Python versus Java. In your last lesson, you learned about access control through properties to control access to certain attributes of your class. In this lesson, we will take a look at another example.
The example I have in mind is a clock. So, a
Clock class might have a field for hours with a value that’s going to be between 1 and 12 and a field for minutes with a value that’s going to be between 0 and 59.
00:49 We would do something similar for hours, we would also have methods to set the time and increment each minute—not the scope of this lesson, so we’re just going to focus on that portion of the class that wants to restrict the value of minute between 0 and 59.
We know they’re non-public by the presence of the underscore (
_). The initializer method provides values through the parameters. And I’ve written a property for minutes that if I refer to
.minutes directly to inquire its value as a property, it will return the value of the non-public attribute
And I can change that. Say I need to get up earlier one morning, set it to 6:15. The setter was called, checked the value of
15, saw that it was between
59 inclusive, and it allowed that value to change.
75, my setter did not produce an error message—that was my choice. Depending on where this clock is being used, an error message may or may not be visible to someone to actually see it, but it did prevent the value of
._minutes from being changed because 6:75 is not a valid time to set your clock.
This new version changes that. I redefine the attribute in the initializer to refer to the property—not a non-public attribute. I then define the property
.minutes. When we use
.minutes directly it’s going to return the value of the non-public attribute, which is actually created the first time a setter is used.
05:14 And so my setter will be used for the very first time during the constructor if the parameter minutes, which is going to be the right-hand operand in an assignment statement, is legal. Again, I set the value of the non-public attribute to that parameter, and if this happens in the constructor call, this is actually created.
So, the non-public attribute doesn’t get created until the constructor call refers to it through the property using its setter. Now, there are two times where this setter might be used, where this test for a legal value could fail. One, the
Clock object has already been created, and we want to ignore the new value since it’s illegal.
But the other time it could happen is during the initializer call, and so we’ll check to see if that attribute actually exists and if that attribute hasn’t been created and we had tried to assign a value that’s illegal for
._minutes, we’ll just set
And that’s still
30, because during the initializer call we set the property value using the setter to
30 was a legal value used here, and so it created this non-public attribute and assigned it to the value
30, which was passed by the parameter to the initializer.
again, no error message reported, by my choice. In your context, if you want an error message displayed, you can do that. But I can see that wasn’t changed. Now, if I want to try to create a new clock… We’ll call this
clock2, and again, try to set it for 7:65 in the morning, whatever that would possibly mean.
So, there are two versions of a
Clock class, which prevent illegal values to be given to the
._minutes. In a fully implemented class, I would do the same thing with
._hours to keep them between
12, or between
23 if you would prefer a 24-hour clock. In our next lesson, we’ll take a look at Python’s use of the word
Become a Member to join the conversation.