Python Programming - Encapsulation - Discussion

Discussion Forum : Encapsulation - General Questions (Q.No. 73)
73.
Consider the following Python code:
class Product:
    def __init__(self, name, __price):
        self.name = name
        self.__price = __price

    def set_price(self, new_price):
        if new_price > 0:
            self.__price = new_price
What is the purpose of the set_price() method?
To retrieve the price of the product
To set a new price for the product with validation
To create a new instance of the class
To expose all internal details of the class
Answer: Option
Explanation:
The set_price() method allows setting a new price for the product with validation, demonstrating encapsulation.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.