469,356 Members | 2,033 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

Really virtual properties

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.

--
Torsten Bronger, aquisgrana, europa vetus
Aug 18 '05 #1
5 1888
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

Aug 18 '05 #2
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

Aug 18 '05 #3
Torsten Bronger <br*****@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
Aug 19 '05 #4
On Thu, 18 Aug 2005 23:36:58 +0200, Torsten Bronger <br*****@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)

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):
class RVP(object): ... def __init__(self, gettername):
... self.gettername = gettername
... def __get__(self, inst, cls=None):
... if inst is None: return self
... return getattr(inst, self.gettername)()
... class Base(object): ... def get_x(self): return 'Base get_x'
... x = RVP('get_x')
... class Derv(Base): ... def get_x(self): return 'Derv get_x'
... b = Base()
d = Derv()
b.x 'Base get_x' d.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.,
class Base(object): ... x = property(lambda self: 'Base get_x')
... class Derv(Base): ... x = property(lambda self: 'Derv get_x')
... b = Base()
d = Derv()
b.x 'Base get_x' d.x

'Derv get_x'

Regards,
Bengt Richter
Aug 19 '05 #5
Ben Finney wrote:
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
Aug 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by aap | last post: by
3 posts views Thread by Smitha Nataraj | last post: by
7 posts views Thread by Jaydeep | last post: by
4 posts views Thread by Just D. | last post: by
1 post views Thread by sudhapadmas | last post: by
6 posts views Thread by Scott M. | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.