Python Programming - Encapsulation - Discussion

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