Accessing object parent properties

bruno de chez modulix en face bruno at modulix.org
Fri May 26 08:37:47 EDT 2006


(answering to the op)
Cloudthunder wrote:
> How can I set up method delegation so that I can do the following:

> A.run()

> and have this call refer to the run() method within the boo instance? Also,
> what if I have tons of functions like run() within the boo instance and I
> want all them to be directly accessible as if they were part of their
> parent
> (the Foo instance)?

class Foo(object):
  def __init__(self, delegate):
    self._delegate = delegate

  def __getattr__(self, name):
    try:
      return getattr(self._delegate, name)
   except AttributeError:
      msg = "object '%s' has no attribute '%s' %
(self.__class__.__name__,

    name)
     raise AttributeError(msg)

class Boo(object):
   # your code here
   def run(self):
      print "This is a job for SuperBicycleRepairMan"

f = Boo(Foo())
f.run()

HTH




More information about the Python-list mailing list