Python Programming - Encapsulation - Discussion

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