[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] offtopic - inheritance and property()



I guess a bit off topic question pretty general about python. After few weeks facing this problem and searching for solutions online I decided to ask the pygame list.

Basically i have some objects that have self.x, and self.y properties. They are implemented as property() with setter and getter because there are som calculations to be done apart from just setting the values. Now I have the problem that some special objects that inherit the x and y props need to do some more stuff when the x and y are set. Few month ago i didnt use the property() but a java-like setter so i could easily extend this methods when i faced this kind of problem. But recently I implemented the more pythonic property() system and now I find that extending the methods wont work because of the way property() works.

So i am trying to find solutions to this but so far the only way it is too completely overwrite the setter and getter and create again the property() in the subclass. So i have to repeat all the code from the superclass ... I also tried some hacks. They kind of work but they feel to me like too weird to be a good solution.

Could anyone suggest some nicer way to be able to extend those inherited methods while still using the property() system?

I'm using something like this:

class A:
	def set_x(self, x): ...
	def get_x(self): ...
	x = property(get_x, set_x)

class B(A)
	def set_x(self, x):
		A.set_x(self, x)
		...
	def get_x(self)
		x = A.get_x(self)
		...
		return x
	x = property(get_x, set_x)

-- Karl