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

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



I've also noticed the annoying problems of properties and inheritance.

I found a solution in this informative presentation:
  http://www.aleax.it/Python/osc05_bla_dp.pdf

The solution is:
  class Base(object):
    def getFoo(self): return 23
    def _property_getFoo(self): return self.getFoo()
    foo = property(_property_getFoo)
  class Derived(Base):
    def getFoo(self): return 42

It's a little ugly, but it's not too bad.

On Wed, 2006-01-11 at 11:29 +0100, altern wrote:
> hi all
> 
> 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?
> 
> thanks and sorry again for kind of off topic
> 
> --
> enrike
>