0% completed
Python - Access Modifiers
On This Page
Public Attributes
Example
Private Attributes
Example
Protected Attributes
Example
Access modifiers are key concepts in object-oriented programming that control the visibility of class members (attributes and methods). Python's approach to access modifiers is unique because it does not have explicit keywords like private, protected, or public found in some other languages. Instead, Python uses naming conventions to indicate the intended level of access for class members.
Public Attributes
Public attributes are accessible from anywhere, both inside and outside of the class. In Python, any attribute without a leading underscore is considered public by default.
Example
In this example, we create a Car class with public attributes to demonstrate how they can be accessed freely within and outside of the class.
Explanation:
self.makeandself.modelare declared without any underscores, making them public.my_car = Car("Toyota", "Corolla"): Instantiates theCarclass.- The print statement directly accesses the public attributes
makeandmodel.
Private Attributes
Private attributes are intended to be hidden from outside class access. In Python, you denote private attributes by prefixing the name with two underscores (__), which also enables name mangling to further protect these attributes from external access.
Example
In this example, we demonstrate defining private attributes in the Car class and accessing them through a class method.
Explanation:
self.__makeandself.__modelare private due to the double underscore prefix.display_info()is a method that safely accesses these private attributes within the class.- Attempting to access these attributes directly from the instance outside the class (like
my_car.__make) would result in an error due to Python’s name mangling.
Protected Attributes
Protected attributes are meant to be accessed within the class and subclasses but should be considered private from an outside perspective. They are indicated by a single underscore (_) prefix.
Example
In this example, the Vehicle class demonstrates protected attributes accessed by a subclass.
Explanation:
self._typein theVehicleclass is a protected attribute indicated by a single underscore.- The
Carclass, which inherits fromVehicle, can access this protected attribute in itsdisplaymethod to show how subclasses can interact with protected members. - This setup illustrates the convention in Python where the single underscore warns other developers that the attribute should be treated as non-public, but it is still technically accessible from outside the class.
This comprehensive look at Python's access modifiers, demonstrated through practical examples, highlights how Python relies on conventions to indicate the intended level of accessibility for class attributes. These conventions help maintain a clean and understandable codebase, especially in large projects or frameworks.
Reading Progress
0%
On This Page
Public Attributes
Example
Private Attributes
Example
Protected Attributes
Example