Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Access Control

00:00 Welcome to lesson five in Object-Oriented Programming in Python versus Java. In your last lesson, we learned that everything in Python is public. In this lesson, you’ll learn the Python tools that will allow you to control access to the attributes in your class. Everything in Python is public, but what if you really need to control access to an attribute? For example, prevent it from getting an illegal value or being constructed inappropriately?

00:28 Python does have a mechanism for that called a property. The syntax is Python’s decorator syntax, which is similar to Java’s annotation syntax. You use the decorator @property to define a property name in terms of an existing instance variable, and then you can write methods to use when trying to set or delete that particular attribute. Remember, you can create an attribute for an object after you’ve created it by simply defining it. If you want to remove an attribute from an object, you have that ability as well. And if you—the designer of a class—want to enforce certain things when an attribute you’ve created in the class is about to be deleted, you can write a method for that as well.

01:18 Defining the property actually defines your getter method, and so you don’t need to write a separate one, although Python syntax allows it. This looks a little bit like C#’s concepts of properties with its set and get accessors, so if you have some familiarity with C#, you may have seen things like this before.

01:42 Let’s take a look at our Car example.

01:47 Here, we’ve created a new attribute, non-public, called ._voltage, and we would like to control access to this. So we create a property and we define a method, .voltage(), which becomes our getter, so to speak.

02:07 And again, since every Python class method must have a reference to self, it has that parameter. And so if we use the term .voltage, we would like to use that to return the value of that non-public attribute. If we want to provide a setter for it, we would say @voltage, using the new property name, .setter.

02:30 We create a method called .voltage(), and inside that method we provide whatever action we want. Assuming that we’re actually going to change the value of ._voltage, we will take a second parameter for that new value.

02:43 And in this particular case, changing the voltage level of the car could perhaps have difficulties, and so we print out a warning to say that that can cause problems.

02:55 And again, we can create a deleter by using @voltage, our new property name, .deleter and creating with this decorator a method called .voltage(), which will warn us that the radio will stop working if we should happen to remove voltage from the car.

03:14 And so here is a typical example. And now let’s go ahead and look at its use.

03:23 So, let’s import car and create a Car.

03:31 There’s my 2018 silver Ford Fusion again. And if I wish to use the property to get to the instance variable, I would say something like print(f"My car uses {}"), and if I say my_car.voltage, I’m using the property name—not the non-public attribute name.

03:57 It will find the property called .voltage, see that that property returns the value in the non-public attribute ._voltage, and it will tell us that the car uses 12 volts.

04:11 If I decide to change that—going through the property, just creating an assignment statement, I’m going to assign this to say 9 volts. The method I wrote does that reassignment but it also prints out a warning that if the battery level drops from 12 volts to 9 volts, that could be a problem. It has changed it.

04:33 If I scroll back and repeat this print statement,

04:38 I can see that that car has indeed had its voltage changed.

04:44 I can use the del command to actually remove that property. In other words, I no longer want my_car to have a ._voltage attribute.

04:54 Because I’ve written a property that carries out that task, it will do that, but it will now tell me that without any voltage things in the car aren’t going to work—the radio will stop working.

05:06 If I try now to find the value of ._voltage, I get an error message that tells me that the attribute ._voltage no longer exists. So again, let’s go back and refresh our memory about what the code looked like. There’s our assignment statement, a typical setter.

05:26 And here’s where we delete the non-public attribute once given the request to delete the property. And so from outside the class, we are using the typical dot (.) and then property name.

05:44 From here, we can’t see the difference between calling it a property or a field because it doesn’t begin with an underscore (_) in either case, and so that makes our use consistent.

05:57 In your next lesson, we’ll take a look at another example of access control for a different Python class.

Become a Member to join the conversation.