Python Programming - Encapsulation - Discussion

Discussion Forum : Encapsulation - General Questions (Q.No. 66)
66.
How can encapsulation be enforced to make a variable 'password' accessible only within its own class?
class User:
    def __init__(self, username, password):
        self.username = username
        self.__password = password
Add a single underscore prefix before 'password'
Add a double underscore prefix before 'password'
Use a getter method for 'password'
Make 'password' a global variable
Answer: Option
Explanation:
Adding a double underscore prefix before 'password' 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.