Property in derived class

J

Joseph Turian

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...dc8/9d32049aad12e1c1?lnk=gst#9d32049aad12e1c1

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
 
I

Ian Kelly

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"
 
G

George Sakkis

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.lang.python/browse_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 Python's property.xhtml
 

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
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top