Last Updated: January 3, 2026
Understanding how to control access to your class attributes and methods is essential in Python, especially when you think about maintaining clean and manageable code. The concepts of public, private, and protected members help encapsulate the data and methods in a way that promotes the integrity of your code.
Let’s dive into these access modifiers and see how they work in Python.
In Python, public members are accessible from outside the class. This means you can freely use these attributes and methods without any restrictions. By default, all class members are public unless specified otherwise.
Public members are great for exposing an interface that other parts of your code can interact with. They allow for easy access and manipulation, making your class straightforward to use. However, overexposing your class members can lead to potential misuse or unintended side effects.
Consider the following example:
In this example, the make and model attributes, as well as the display_info method, are public. Anyone using the Car class can easily access and modify them.
Use public members when you intend for other classes or modules to access them directly. However, remember to validate inputs and maintain the internal state of your class when exposing such methods or attributes.
Next up, we have protected members. While Python does not enforce access restrictions, the convention is to prefix protected members with a single underscore (_). This signals to other developers that these attributes or methods are intended for internal use only and should not be accessed directly outside the class hierarchy.
Protected members help to create a controlled environment within a class and its subclasses. This is particularly useful in situations where you want to allow derived classes to access certain attributes, while discouraging external access.
Here's an example to illustrate protected members:
In this scenario, _make and _model are marked as protected. The Car subclass can safely access these attributes, but external code should avoid doing so.
Protected members are beneficial when you have a class hierarchy and want to allow subclasses to access certain internals without exposing them to the rest of the world. It’s a way of saying, “I trust you with this, but please be cautious.”
Now, let’s discuss private members. These are meant to be fully encapsulated within the class. In Python, private members are indicated by a double underscore prefix (__). This triggers name mangling, which effectively changes the name of the member in a way that makes it harder to accidentally override or access from outside the class.
Private members are perfect for protecting your data from unintended interference. They ensure that the internal workings of a class remain hidden, promoting loose coupling and reducing the risk of errors stemming from external access.
Here’s how private members work:
In this example, __brand and __display_brand cannot be accessed directly from outside the Bike class, which enhances encapsulation.
Use private members when you want to ensure absolute control over how an attribute or method is used. They are particularly useful when you want to maintain an invariant or enforce certain conditions.
It’s essential to understand how to access these members, especially private ones, which use name mangling. When you define a private member in a class, Python changes its name to include the class name.
Here’s how you can access a private member from outside the class:
While this is technically possible, you should avoid doing so. This breaks encapsulation and can lead to code that is difficult to maintain.
Protected members can be accessed from subclasses but should generally be avoided from outside classes:
In this example, _protected is accessible within Child, adhering to the intended use of protected attributes.
To wrap it up, let’s summarize the access modifiers we’ve covered:
Understanding these access modifiers allows you to structure your classes more effectively, leading to cleaner, more maintainable code.
Now that you understand public, private, and protected members, you're ready to explore the next topic: name mangling.
In the next chapter, we will look at how Python manages private attributes and methods behind the scenes, ensuring that your encapsulation strategy remains robust and effective.