Property in derived class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joseph Turian

    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:


    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
  • Larry Bates

    #2
    Re: Property in derived class

    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:

    >
    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__(s elf, 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

    Comment

    • Ian Kelly

      #3
      Re: Property in derived class

      On Fri, May 9, 2008 at 3:20 PM, Joseph Turian <turian@gmail.c omwrote:
      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 indirectpropert y class. I haven't thoroughly tested this,
      so I don't know whether it works exactly right.

      class indirectpropert y(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(instanc e, self.sget)
      else:
      fget = getattr(owner, self.sget)
      return fget()

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

      def __delete__(self , instance):
      fdel = getattr(instanc e, self.sdel)
      fdel()


      class Foo(object):
      def func(self): return "foo"
      callfunc = indirectpropert y(sget="func")

      class Bar(Foo):
      def func(self): return "bar"

      Comment

      • George Sakkis

        #4
        Re: Property in derived class

        On May 9, 5:20 pm, Joseph Turian <tur...@gmail.c omwrote:
        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(obj ect):

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

        foo = OProperty(_getF oo, _setFoo)

        HTH,
        George


        [1] http://infinitesque.ne t/articles/2005/enhancing%20Pyt hon's%20propert y.xhtml

        Comment

        • Joseph Turian

          #5
          Re: Property in derived class

          On May 9, 9:05 pm, George Sakkis <george.sak...@ gmail.comwrote:
          Using the overridable property recipe [1],
          [1]http://infinitesque.ne t/articles/2005/enhancing%20Pyt hon's%20propert y...
          Thanks, this is a great solution!

          Joseph

          Comment

          Working...