Python Programming - Encapsulation - Discussion

Discussion Forum : Encapsulation - General Questions (Q.No. 99)
99.
What is the primary purpose of the following Python class?
class Person:
    def __init__(self, name, __age):
        self.name = name
        self.__age = __age

    def get_age(self):
        return self.__age
To create a new instance of the class
To provide controlled access to the 'age' variable
To define a public variable 'age'
To allow unrestricted access to the 'age' variable
Answer: Option
Explanation:
The get_age() method provides controlled and read-only access to the private variable '__age', demonstrating encapsulation.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.