+ 12

@property and <bound method> question

I've been experimenting with the @property decorator but getting some puzzling results from this code (see the comments). Any Pythonista wisdom would be welcome 😁 https://code.sololearn.com/ct0o1KLzZE09/?ref=app

27th Nov 2017, 4:47 AM
David Ashton
David Ashton - avatar
5 Antworten
+ 7
''' I found this solution on internet, tried and it worked well. Here link: https://stackoverflow.com/questions/4183432/cant-set-attribute-with-new-style-properties-in-JUMP_LINK__&&__python__&&__JUMP_LINK ''' @property def pineapple_number(self): return 6 @pineapple_number.setter def pineapple_number(self,value): 'setting' self = value
27th Nov 2017, 3:26 PM
Ferhat Sevim
Ferhat Sevim - avatar
+ 7
@Ferhat and @pcrlA3 Thanks very much - that helps!
29th Nov 2017, 1:59 AM
David Ashton
David Ashton - avatar
+ 7
Generally the caller of the class should not need any knowledge about internals of the class. So in this case he should not need knowledge, how a property is implemented. (you did on two different ways) He should be able to access mushrooms_number the same way as pineapple_number. So - you want to call 55.() ---> no - you actually dont want. Thanks for the question! Writing it down helps me too! Please correct if sth. is wrong
29th Nov 2017, 10:19 AM
Oma Falk
Oma Falk - avatar
+ 6
basically, @property is a getter by default. that's why the object is not callable. "@property def pineapple_number(self): return 6" is really the same as "pineapple_number = property(pineapple_number)"
27th Nov 2017, 2:28 PM
🐙evil octopus
🐙evil octopus - avatar
0
1 thing sololearn should include https://www.sololearn.com/discuss/813347/?ref=app
28th Nov 2017, 1:52 PM
Fire Feathers
Fire Feathers - avatar