Tricky Areas in Python

Steven Bethard steven.bethard at gmail.com
Mon Oct 24 11:59:43 EDT 2005


Alex Martelli wrote:
> 
>>>    class Base(object)
>>>        def getFoo(self): ...
>>>        def setFoo(self): ...
>>>        foo = property(getFoo, setFoo)
>>>
>>>    class Derived(Base):
>>>        def getFoo(self): ....
>>
[snip]
> the solution, in Python 2.4 and earlier, is to use
> one extra level of indirection:
>     def __getFoo(self): return self.getFoo()
>     def getFoo(self): ...
>     foo = property(__getFoo)
> so the name lookup for 'getFoo' on self happens when you access s.foo
> (for s being an instance of this here-sketched class) and overriding
> works just as expected.

Another solution (for those of you scoring at home) would be to use a 
property-like descriptor that delays the name lookup until the time of 
the method call, e.g.

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/442418
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/408713

STeVe



More information about the Python-list mailing list