+ 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
9 odpowiedzi
0
health is not a variable. it is an object, the same as maxHealth.
0
@yuri isn't it an attribute of the class?
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).
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
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
0
now, health is an attribute of the class. it is similar as for list : b = a, or b=a[:]
0
@yuri in a[:], colon is used to slice the list, isn't it?
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.
0
@yuri ooh ok now i understand! thank you so much!