python - Assign attributes when another is changed -


i have python shape class attribute called datum. list 2 numbers, x , y.

if

my_shape = shape() 

then datum given default value: [0,0] now, want reassign datum doing:

my_shape.datum = [3,2]  

then datum assigned list.

but have attributes x , y. how make x , y automatically update become first , second item of datum list?

i put in init

self.x = self.datum[0] self.y = self.datum[1] 

but assigns x , y initialized values , can't figure out how update when self.datum updates.

i'm sorry if confusing. thank helping newbie.

-jason

you can define x , y properties:

@property def x(self):     return self.datum[0]  @x.setter def x(self, value):     self.datum[0] = value  @property def y(self):     return self.datum[1]  @y.setter def y(self, value):     self.datum[1] = value 

this way, x , y not attributes (and data stored once in datum), stay usable such (i.e.: my_shape.x , my_shape.y works expected).


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -