Python Programming - Encapsulation - Discussion

Discussion Forum : Encapsulation - General Questions (Q.No. 92)
92.
How can encapsulation be enforced in Python to make a variable 'employee_id' accessible only within its own class?
class Employee:
    def __init__(self, employee_id, __salary):
        self.employee_id = employee_id
        self.__salary = __salary
Use a getter method for 'employee_id'
Add a single underscore prefix before 'employee_id'
Make 'employee_id' a global variable
Add a double underscore prefix before 'employee_id'
Answer: Option
Explanation:
Adding a double underscore prefix before 'employee_id' makes it a private variable, enforcing encapsulation within the class.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.