Really virtual properties

T

Torsten Bronger

Hallöchen!

When I use properties in new style classes, I usually pass get/set
methods to property(), like this:

x = property(get_x)

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?

(I found the possibility of using an intermediate method _get_x
which calls get_x but that's ugly.)

Tschö,
Torsten.
 
D

Diez B. Roggisch

I don't think so - the reason is that property(<getter>) is evaluated
in the baseclass, and stores a callable, not a name. the only thing you
could do is either

- create a level of indirection, using lambda, to force the lookup:

x = property(lamda self: self.get_x())

- use a metaclass, that tries to scan the baseclass for properties
that uise functionnames which are redefined in the current class, and
recrerate a new property for those.

Diez
 
D

Diez B. Roggisch

I don't think so - the reason is that property(<getter>) is evaluated
in the baseclass, and stores a callable, not a name. the only thing you
could do is either

- create a level of indirection, using lambda, to force the lookup:

x = property(lamda self: self.get_x())

- use a metaclass, that tries to scan the baseclass for properties
that uise functionnames which are redefined in the current class, and
recrerate a new property for those.

Diez
 
B

Ben Finney

Torsten Bronger said:
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."
said:
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>
 
B

Bengt Richter

Hallöchen!

When I use properties in new style classes, I usually pass get/set
methods to property(), like this:

x = property(get_x)

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?

(I found the possibility of using an intermediate method _get_x
which calls get_x but that's ugly.)
I think this idea of overriding a property access function is ugly in any case,
but you could do something like this custom descriptor (not tested beyond
what you see here):
... def __init__(self, gettername):
... self.gettername = gettername
... def __get__(self, inst, cls=None):
... if inst is None: return self
... return getattr(inst, self.gettername)()
... ... def get_x(self): return 'Base get_x'
... x = RVP('get_x')
... ... def get_x(self): return 'Derv get_x'
... 'Derv get_x'

But why not override the property x in the derived subclass instead,
with another property x instead of the above very questionable trick? I.e.,
... x = property(lambda self: 'Base get_x')
... ... x = property(lambda self: 'Derv get_x')
... 'Derv get_x'

Regards,
Bengt Richter
 
S

Steven Bethard

Ben said:
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>

Also see Tim Delaney's comment at the bottom, which provides similar
functionality by subclassing property.

STeVe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,263
Messages
2,571,312
Members
47,987
Latest member
Gaurav

Latest Threads

Top