Can you explain the meaning of data hiding here? I also can't understand what does setter do here, and what the code after the setter method does.
My interpretation:
- We create an instance called pizza of the class Pizza, with its toppings-attribute equal to the list of strings given by ["cheese", "tomato"].
- When we use the code print(pizza.pineapple_allowed), it goes to that property method, making it a read-only attribute, and then returns to self thus returning to False.
I can't understand after that. Please Help!
class Pizza:
def __init__(self, toppings):
self.toppings = toppings
self._pineapple_allowed = False
@property
def pineapple_allowed(self):
return self._pineapple_allowed
@pineapple_allowed.setter
def pineapple_allowed(self, value):
if value:
password = input("Enter the password: ")
if password == "Sw0rdf1sh!":
self._pineapple_allowed = value
else:
raise ValueError("Alert! Intruder!")
pizza = Pizza(["cheese", "tomato"])
print(pizza.pineapple_allowed)
pizza.pineapple_allowed = True
print(pizza.pineapple_allowed)