Tricky Areas in Python

Fredrik Lundh fredrik at pythonware.com
Mon Oct 24 12:36:18 CEST 2005


Steven D'Aprano wrote:

> Those two are easy. However, and this is where I show
> my hard-won ignorance, and admit that I don't see the
> problem with the property examples:

>>     class Base(object)
>>         def getFoo(self): ...
>>         def setFoo(self): ...
>>         foo = property(getFoo, setFoo)
>>
>>     class Derived(Base):
>>         def getFoo(self): ....

the property call in Base binds to the Base.getFoo and Base.setFoo method
*objects*, not the names, so overriding getFoo in Derived won't affect the foo
property.

to get proper dispatching for accessors, you need to add an extra layer:

            def getFoo(self): ...
            def setFoo(self, ...): ...
            def getFooDispatcher(self): self.getFoo() # use normal lookup
            def setFooDispatcher(self, ...): self.setFoo(...) # use normal lookup
            foo = property(getFooDispatcher, setFooDispatcher)

this gotcha is the motivation for this proposal:

    http://article.gmane.org/gmane.comp.python.devel/72348

(see that thread for more on this topic)

</F> 






More information about the Python-list mailing list