Really virtual properties

Ben Finney bignose+hates-spam at benfinney.id.au
Thu Aug 18 20:02:20 EDT 2005


Torsten Bronger <bronger at physik.rwth-aachen.de> wrote:
> Hallöchen!
> 
> When I use properties in new style classes, I usually pass get/set
> methods to property(), like this:
> 
>     x = property(get_x)

Better is to make it clear that 'get_x' is not intended to be called
directly. You can do this through the convention of naming the
function '_get_x', or use this recipe for a namespace-clean approach:

    Sean Ross:
    "This recipe suggests an idiom for property creation that avoids
    cluttering the class space with get/set/del methods that will not
    be used directly."
    <URL:http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/205183>

> If I overwrite get_x in a derived class, any access to x still calls
> the base get_x() method.  Is there a way to get the child's get_x()
> method called instead?

Not using the built-in property type. Here is a recipe for a
LateBindingProperty that does what you ask:

    Steven Bethard:
    "This recipe provides a LateBindingProperty callable which allows
    the getter and setter methods associated with the property to be
    overridden in subclasses."
    <URL:http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/408713>

-- 
 \          "Any sufficiently advanced bug is indistinguishable from a |
  `\                                       feature."  -- Rich Kulawiec |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list