473,386 Members | 1,668 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,386 software developers and data experts.

Property in derived class

If I have a property in a derived class, it is difficult to override
the get and set functions: the property's function object had early
binding, whereas the overriden method was bound late.
This was previously discussed:
http://groups.google.com/group/comp....32049aad12e1c1

Could someone demonstrate how to implement the proposed solutions that
allow the property to be declared in the abstract base class, and
refer to a get function which is only implemented in derived classes?

Thanks,
Joseph
Jun 27 '08 #1
4 1719
Joseph Turian wrote:
If I have a property in a derived class, it is difficult to override
the get and set functions: the property's function object had early
binding, whereas the overriden method was bound late.
This was previously discussed:
http://groups.google.com/group/comp....32049aad12e1c1

Could someone demonstrate how to implement the proposed solutions that
allow the property to be declared in the abstract base class, and
refer to a get function which is only implemented in derived classes?

Thanks,
Joseph
Sounds a little like you are trying to write Java in Python.

1) You don't need get/set functions to get/set properties in Python. You just
get the property directly using dotted notation.

2) Properties defined in base class exist in derived class unless you override them.
class foobase(object):
def __init__(self):
self.myProperty = 1

class foo(foobase):
self.__init__(self, myProperty=None)
foobase.__init__(self)
if myProperty is not None:
self.myProperty = myProperty
obj = foo()
print obj.myProperty
>>1
obj = foo(6)
print obj.myProperty
>>6
obj.myProperty = 19
print obj.myProperty
>>10
I hope this was what you were looking for. If not, I don't understand the question.

-Larry
Jun 27 '08 #2
On Fri, May 9, 2008 at 3:20 PM, Joseph Turian <tu****@gmail.comwrote:
Could someone demonstrate how to implement the proposed solutions that
allow the property to be declared in the abstract base class, and
refer to a get function which is only implemented in derived classes?
One way is to have the property refer to a proxy that performs the
late binding, which might look something like this:

def _bar(self):
return self.bar()

prop = property(fget=_bar)

Another way is to declare properties using something like the
following indirectproperty class. I haven't thoroughly tested this,
so I don't know whether it works exactly right.

class indirectproperty(object):

def __init__(self, sget=None, sset=None, sdel=None):
self.sget = sget
self.sset = sset
self.sdel = sdel

def __get__(self, instance, owner):
if instance is not None:
fget = getattr(instance, self.sget)
else:
fget = getattr(owner, self.sget)
return fget()

def __set__(self, instance, value):
fset = getattr(instance, self.sset)
fset(value)

def __delete__(self, instance):
fdel = getattr(instance, self.sdel)
fdel()
class Foo(object):
def func(self): return "foo"
callfunc = indirectproperty(sget="func")

class Bar(Foo):
def func(self): return "bar"
Jun 27 '08 #3
On May 9, 5:20*pm, Joseph Turian <tur...@gmail.comwrote:
If I have a property in a derived class, it is difficult to override
the get and set functions: the property's function object had early
binding, whereas the overriden method was bound late.
This was previously discussed:
* *http://groups.google.com/group/comp....thread/thread/...

Could someone demonstrate how to implement the proposed solutions that
allow the property to be declared in the abstract base class, and
refer to a get function which is only implemented in derived classes?
Using the overridable property recipe [1], it can be written as:

class AbstractFoo(object):

def _getFoo(self):
raise NotImplementedError('Abstract method')
def _setFoo(self, signals):
raise NotImplementedError('Abstract method')

foo = OProperty(_getFoo, _setFoo)

HTH,
George
[1] http://infinitesque.net/articles/2005/enhancing%20Python's%20property.xhtml
Jun 27 '08 #4
On May 9, 9:05 pm, George Sakkis <george.sak...@gmail.comwrote:
Using the overridable property recipe [1],
[1]http://infinitesque.net/articles/2005/enhancing%20Python's%20property...
Thanks, this is a great solution!

Joseph
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Marc L'Ecuyer | last post by:
I want to implement a collection property inside another (in a control derived class) but I have some persistence problems. There is an example of my ColumnItem class ...
7
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
9
by: Christian Kaiser | last post by:
Hmmm. I just got the following compiler messages: test_cpp.cpp d:\src\logsolar\datasources\test_cpp\test_cpp.h(18) : error C3760: please use __property keyword to declare property in managed...
2
by: garyusenet | last post by:
I could do with something similiar, can you tell me if you think this would work for me, and if there's any advantage in working with controls this way than how I currently am. At the moment...
14
by: Dom | last post by:
Hi all I'm developing a control, and I need to hide some properties to the user. For example, suppose I need Text property to be completely inacessible (from a Form/Code that is into another...
1
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I want to confirm my understanding about access modifier for property. Assembly One contains: Class One has Protected property Protected1_prop Class Two has Friend property Friend2_prop Class...
0
by: anileshlakhtakia | last post by:
Hi I have a class let say , Class A { public IList animal { get{}set{} } } Class B:A
4
by: ocramedl | last post by:
Hi all, I work in C# and I have created a custom panel (called CustomPanel) which inherits from UserControl. I have added to my CustomPanel a protected property, called Title. The goal of such...
2
by: parez | last post by:
how do i put an abstract property ( or something that lets me know that derived class has not implemented that particular property ) in base form in my win forms project? if i make the base form...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.