inherit and overwrite a property (better its method call)

chris csad7 at yahoo.com
Mon Feb 16 14:10:07 EST 2004


hi,
i am tinkering with properties of new style classes:

class Base(object):

     def m(self):
         return 'p of Base'
     p = property(m)

class Sub(Base):
     def m(self):
         return 'p of Sub'

b = Base()
print b.p # prints 'p of Base'

s = Sub()
print s.p # prints 'p of Base'!?

i was thinking s.p would use the method m of class Sub and not Base. but 
this does not work, both properties "p" of Base and Sub use method m of 
baseclass Base.

so it seems i cannot overwrite the method p calls to get its value 
without actually repeating the property definition in every subclass, or 
is there a way? the following does work but i want to get rid of the 
second p = property(m)...

class Sub(Base):
     def m(self):
         return 'p of Sub'
     p = property(m)

print b.p # prints 'p of Base'
s = Sub()
print s.p # prints 'p of Sub'

am i missing something?


thanks for any advice
chris




More information about the Python-list mailing list