Web
Opt
tools
Protected Attributes Config
Class Name
Protected Attribute Name
Will be prefixed with underscore (_) automatically
Operation
Create Object
Get Protected Attribute
Set Protected Attribute
Call Method
Direct Access Test
Initial Value
Custom Method Name
Optional: Add a custom method to demonstrate protected attribute access
Run Visualization
Console Output
>
Protected Attributes Visualization
Current Configuration:
Class:
MyClass
Protected Attribute:
_$data
Operation:
create
Value:
1000
Method:
get_data
Protected Attribute:
Protected Attribute
_$data (Single Underscore Prefix)
Convention-based protection indicating 'internal use' Can be accessed directly but should be treated as private
Protected Attribute Characteristics:
Single underscore prefix (_)
Naming convention, not enforced by Python
Can be accessed directly but should be avoided
Indicates 'internal use' to other developers
Recommended Access Methods:
obj.get_data()
Getter Method
obj.set_data(value)
Setter Method
obj._data
Direct Access (Not Recommended)
Operation Status:
Object Created
Instance of MyClass created successfully
Protected vs Private Attributes:
Protected (_attr)
• Single underscore prefix
• Convention only
• Can be accessed directly
• Indicates 'internal use'
Private (__attr)
• Double underscore prefix
• Name mangling enforced
• Cannot be accessed directly
• True encapsulation
Code Structure:
class MyClass:
def __init__(self):
self._data = 1000
def get_data(self):
return self._data
def set_data(self, value):
self._data = value
def get_data(self):
return f"Protected data: {self._data}"
Best Practices:
Use getter and setter methods to access protected attributes
Treat protected attributes as private in your code
Document the intended use of protected attributes
Avoid direct access to protected attributes from outside the class