Python Programming - Encapsulation - Discussion

Discussion Forum : Encapsulation - General Questions (Q.No. 65)
65.
Consider the following Python class:
class BankAccount:
    def __init__(self, balance):
        self._balance = balance

    @property
    def balance(self):
        return self._balance
What does the @property decorator do in this code?
It creates a new instance of the class
It defines a private variable
It provides a setter method for the balance variable
It allows read-only access to the balance variable
Answer: Option
Explanation:
The @property decorator is used to create a read-only property 'balance', providing controlled access to the private variable '_balance'.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.