Python Programming - Encapsulation - Discussion

Discussion Forum : Encapsulation - General Questions (Q.No. 70)
70.
What is the purpose of the following Python code?
class Employee:
    def __init__(self, name, __salary):
        self.name = name
        self.__salary = __salary

    @property
    def salary(self):
        return self.__salary
To create a new instance of the class
To define a public variable 'salary'
To allow unrestricted access to the 'salary' variable
To provide read-only access to the 'salary' variable
Answer: Option
Explanation:
The @property decorator creates a read-only property 'salary', providing controlled access to the private variable '__salary'.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.