what i don't understand about the setter property in python based on the example is, how do we decide what the value is? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

what i don't understand about the setter property in python based on the example is, how do we decide what the value is?

pineapple_allowed was given as False at the beginning, then as True at the end. How did the setter selct True as equal to value?

13th Aug 2017, 8:41 AM
Owolawi Kehinde
Owolawi Kehinde - avatar
5 ответов
+ 4
Please include your code in the post, thanks.
13th Aug 2017, 9:32 AM
Ipang
+ 3
Q: How did the setter select True as equal to value? A: The setter accepts True from this property assignment call, you assigned True for the property by calling this: pizza.pineapple_allowed = True Suggestion: 1) The code for setter is currently only working (executed) if you assigned True, because of the "if value:" check. If you assign the setter with False none of the code will be executed. 2) Since the property assignment is supposed to be password protected, I suggest you to ask for password first, then, if password is correct you decide the next step. algo: ask password if correct password allow property value change else intruder alert Hth, cmiiw
14th Aug 2017, 2:34 AM
Ipang
+ 3
It's this line under the class declaration code that exactly made changes to the pineapple_allowed property: pizza.pineapple_allowed = True <- Argument It invokes the pineapple_allowed setter passing True as argument for the property's value parameter (the setter "value" parameter becomes True) The setter receives the new "value", and since you passed True; it asks for password, after you enter the right password the setter adjusts self._pineapple_allowed to True, because True was what you passed as argument. To test this theory you can change: pizza.pineapple_allowed = True into pizza.pineapple_allowed = False Then see if at the end what the pineapple_allowed will return. Hth,
15th Aug 2017, 5:28 AM
Ipang
0
This is the code associated with my question 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) OUTPUT False Enter the password: Sw0rdf1sh True
13th Aug 2017, 11:27 PM
Owolawi Kehinde
Owolawi Kehinde - avatar
0
Thanks for explaining, but I don't think you've addressed the point of my question. At the beginning of the code self._pineapple_allowed = False In the property definition def pineapple_allowed(self), returns self._pineapple_allowed. In the setter block it is indicated that if password == Sw0rdf1sh!: self._pineapple_allowed = value Then at the end of the code pizza.pineapple_allowed(which should be the same as self.pineapple_allowed) is = True Why does the output print False then after evaluating the password decide that it is True? That's what I'm asking, what exactly made it change from False to True?
14th Aug 2017, 1:37 PM
Owolawi Kehinde
Owolawi Kehinde - avatar