473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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 2167
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ken | last post by:
Hi, We have a component that we've used successfully under W2000 to create virtual directories on an IIS 5.0 web server. We're in the process of upgrading to W2003 and I'm now encountering an...
4
by: aap | last post by:
Hi, I have the following code. #include <iostream> using namespace std; class Root { public: virtual void Root1() = 0; virtual void Root2() = 0;
3
by: Smitha Nataraj | last post by:
Hello, I need to create a Virtual Directory in C#. Pls let me know if you have any hints regarding this.. Thanks, Smitha
7
by: Jaydeep | last post by:
Hi, Anybody knows how to create virtual directory programmatically under root directory ofcourse from code-behind. I am developing web-based application where I need to create a folder and making...
2
by: §iD` | last post by:
Hi! I would like to create a virtual folder (which I want to mount) and populate managing his content by a DLL or something like that in VB.NET (2.0). How can I acomplish this? Thanks to...
4
by: Just D. | last post by:
How can we change the property of one subdirectory of the Virtual Directory? One of the directories should be granted "Write". Should we delete this Virtual Directory to recreate it with the...
2
by: Rose winsle | last post by:
Hi guys ... I just wanted to create virtual directory using VB.net . i can manage that ... when i create the virtual directory how can i set anonymous access off. following is my code...
1
by: sudhapadmas | last post by:
Hello netters, I was trying to create a virtual directory in IIS using the following code: System.EnterpriseServices.Internal.IISVirtualRoot vr = new...
4
by: Gert Kok | last post by:
The microsoft page http://msdn2.microsoft.com/en-us/library/9fkccyh4.aspx states: Remarks (nr 4) Virtual properties behave like abstract methods, except for the differences in declaration...
6
by: Scott M. | last post by:
I didn't get a resolution to this in my earlier post, so I'll try again: System: Windows XP Pro. (SP2) with IIS installed and running PRIOR to VS 2008 Pro. installation. VS 2008 Pro. (full...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.