Loading video player…

Public and Private

00:00 Welcome to lesson four in Object-Oriented Programming in Python versus Java. In this lesson, we explore Python’s notion of public and private attributes.

00:12 And it’s really real simple! In Python, everything is public. Any method you create, any attribute you create—they can all be accessed both inside and outside of the class.

00:26 So, referring to our familiar Car example, I can create a new Car, my silver Ford Fusion, and I can access any of its attributes directly.

00:44 f"The color of my car is {my_car.color}". Using the dot (.) operator like we normally would, we can access that attribute. And also if we want to paint the car—say I want to make it "blue"—I can do that.

01:06 I can see that that change has been made when I access that attribute again. So, in Python, everything is public. And there are some attributes in our class that we would like programmers that use our class to be able to access directly, but you might have some that you would prefer remain hidden or not used outside the class as much as possible.

01:31 Python provides a mechanism for that. If you begin an attribute name with an underscore (_), it is understood by Python programmers that that is supposed to be a hidden or non-public attribute.

01:45 It’s still accessible, but the use of the underscore tells another programmer that if you want to use it, realize that this really isn’t intended to be used outside the class.

01:58 And so in my Car definition, I’ve included a new attribute. This one’s called ._cupholders. And in this model, all cars have 6 cup holders. The use of the underscore means “Treat this as non-public.” Treat this as if you’re not supposed to access it outside of the class. You can, but it’s understood that you aren’t usually going to.

02:24 I can in fact show that. f"My car has {}" and I can say my_car._cupholders with the underscore in front of it.

02:39 My car has 6 cup holders. So, we can access it but because of the underscore, it’s understood you probably won’t need to. If you really want to hide that attribute name, use a second underscore. It’s still public, but Python changes its name so that if you want to access this non-public item outside of the class, you have to really mean it.

03:12 You can’t access it by its two-underscore name—you have to use the new name that Python gives it.

03:23 So, suppose I change this attribute to having two underscores (__) in front of it.

03:31 And I will restart Python over here.

03:39 So, import the new version of car, say my_car = car.Car(). It’s still silver. I didn’t really repaint it.

03:53 And let me repeat back

03:58 this statement so we can see that my_car does exist and it is still silver. Now, this won’t work because I’ve changed the attribute from having one underscore in front of it to two. Nope, can’t find it.

04:14 And even if I retype this line or repeat this line and put the second underscore—nope, still can’t find it. Because, again, Python has changed its name to make it really difficult for you to get to this field. The programmer used two underscores.

04:34 They want it to not be used. If you’re going to use it, you have to really want it. The name it gives is preceding the two underscores in front of the attribute name also comes the class name. And so if I take a look, repeating this line, if I want to get the correct new name that Python gave it,

05:03 it’s going to be my_car, dot (.), and then the attribute name is _Car__cupholders. And now we can access it. And there it is.

05:20 Everything in Python is public. Programmers have agreed to not use an attribute that begins with an underscore outside of the class. And Python itself enforces that a little bit. If you use two underscores, the name will actually be changed. Now, as a programmer, you know how to find it.

05:42 If you really want it, you can get to it. But the person who wrote the original class probably had a good reason not to include it. So, what do you do if it’s really important that an attribute not be accessed directly? For example, we wouldn’t want to change the number of cup holders to -2. That wouldn’t make sense.

06:07 Python does have a mechanism to provide a bit of control to the attributes that you create, and we will take a look at that in our next lesson.

Become a Member to join the conversation.