Get startedGet started for free

Managing data access: private attributes

1. Managing data access: private attributes

In the next two lessons, we'll talk about managing data access.

2. All class data is public

All class data in Python is technically public. Any attribute or method of any class can be accessed by anyone. If you are coming from a background in another programming language like Java, this might seem unusual or an oversight, but it is by design. The fundamental principle behind much of Python design "we are all adults here". It is a philosophy that goes beyond just code, and describes how the Python community interacts with each other: you should have trust in your fellow developers.

3. Restricting access

That said, there are a few ways to manage access to data. We can use some universal naming conventions to signal that the data is not for external consumption; then, there are special kinds of attributes called properties that allow you to control how each attribute is modified. Finally, there are special methods that you can override to change how attributes are used entirely. We'll cover the first two options in this chapter, and you are unlikely to ever need anything beyond that.

4. Naming convention: internal attributes

Let's start with naming conventions. The first and most important convention is using a single leading underscore to indicate an attribute or method that isn't a part of the public class interface, and can change without notice. This convention is widely accepted among Python developers, so you should follow it both as a class developer and as a class user. Nothing is technically preventing you from using these attributes, but a single leading underscore is the developer's way of saying that you shouldn't. The class developer trusts that you are an adult and will be able to use the class responsibly. This convention is used for internal implementation details and helper functions. For example, a pandas DataFrame has an underscore-is_mixed_type attribute that indicates whether the DataFrame contains data of mixed types, and the datetime module contains a _ymd2ord function that converts a date into a number containing how many days have passed since January 1st of year 1.

5. Naming convention: pseudoprivate attributes

Another naming convention is using a leading double underscore. Attributes and methods whose names start with a double underscore are the closest thing Python has to "private" fields and methods of other programming languages. In this case, it means that this data is not inherited - at least, not in a way you're used to, because Python implements name mangling: any name starting with a double underscore will be automatically prepended by the name of the class when interpreted by Python, and that new name will be the actual internal name of the attribute or method. The main use of these pseudo-private attributes is to prevent name clashes in child classes: you can't control what attributes or methods someone will introduce when inheriting from your class, and it's possible that someone will unknowingly introduce a name that already exists in you class, thus overriding the parent method or attribute! You can use double leading underscores to protect important attributes and methods that should not be overridden. Finally, be careful: leading AND trailing double underscores are only used for build-in Python methods like init, so your name should only start -- but not end! -- with double underscores.

6. Let's practice!

Head over to the exercises to review these conventions. In the next video, you'll learn how to add a little more control to how attributes are used.