+ 1

Does a variable inherit readonly property if i assign it value of a readonly variable?

in this code i made a class which has got two attributes: the first can't be changed, the second i would like to assign the same value of the first initially, and change it after, but it raise me this error: 'can't set attribute' i'm not trying to change the readonly property but the other attribute, why does it give me that error? https://code.sololearn.com/clAslsLzaCLc/?ref=app

17th Jul 2017, 6:06 AM
Daniele
Daniele - avatar
9 Respostas
0
health is not a variable. it is an object, the same as maxHealth.
17th Jul 2017, 8:29 AM
yuri
0
@yuri isn't it an attribute of the class?
17th Jul 2017, 9:00 AM
Daniele
Daniele - avatar
0
you use @property. I am not well familiar with it. but when you write health=maxHealth , you make that two objects identical. (in Python, EVERYTHING is an object).
17th Jul 2017, 9:07 AM
yuri
0
@property make the method below itself like a normal attribute, this allows you to read the value returned by the method and forbids you to assign it another value thank you to reply, i'll try to resolve this question in another way
17th Jul 2017, 9:21 AM
Daniele
Daniele - avatar
0
@yuri i tried to re-write the code and it works in this way, but i don't understand the difference between this code and the first i sent hahaha https://code.sololearn.com/clAslsLzaCLc/?ref=app
17th Jul 2017, 9:25 AM
Daniele
Daniele - avatar
0
now, health is an attribute of the class. it is similar as for list : b = a, or b=a[:]
17th Jul 2017, 9:31 AM
yuri
0
@yuri in a[:], colon is used to slice the list, isn't it?
17th Jul 2017, 9:34 AM
Daniele
Daniele - avatar
0
no, b=a means that both refer to the same object. while, b=a[:] creates a copy of a . then a[0] =1 ,will change b[0] in the first case, but not in the second.
17th Jul 2017, 9:38 AM
yuri
0
@yuri ooh ok now i understand! thank you so much!
17th Jul 2017, 9:40 AM
Daniele
Daniele - avatar