Re: Accessing object parent properties

Discussion in 'Python' started by Ryan Forsythe, May 23, 2006.

  1. 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)?


    Are you sure you don't want to use a child class for this?

    In [1]: class A:
    ...: def foo(self): print "A defined me."
    ...:

    In [2]: class B(A):
    ...: def bar(self): print "B defined me."
    ...:

    In [3]: b = B()

    In [4]: b.foo()
    A defined me.

    In [5]: b.bar()
    B defined me.

    Otherwise, for every element of the A object in B you'd have to create
    an accessor function:

    In [12]: class C:
    ....: a = A()
    ....: def foo(self): self.a.foo()
    ....:

    In [13]: c = C()

    In [14]: c.foo()
    A defined me.

    There are ways to do this kind of automatically:

    In [16]: class D:
    ....: a = A()
    ....: def __getattr__(self, name):
    ....: return getattr(self.a, name)
    ....:

    In [17]: d = D()

    In [18]: d.foo()
    A defined me.


    .... but I doubt you need them.
     
    Ryan Forsythe, May 23, 2006
    #1
    1. Advertising

  2. > Are you sure you don't want to use a child class for this?

    Composition/delegation introduce far less coupling than implementation
    inheritance. Inheritance abuse is in fact a well-known OO antipattern.
    Since Python makes delegation easy as pie, I don't see any reason to go
    for inheritence when it's not really needed (FWIW, you'll notice that
    Python class hierarchies tend to be really flat when compared to Java
    or like).
     
    bruno de chez modulix en face, May 26, 2006
    #2
    1. Advertising

  3. (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
     
    bruno de chez modulix en face, May 26, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilyas
    Replies:
    1
    Views:
    313
    Softwaremaker
    Jul 4, 2003
  2. jon wayne
    Replies:
    9
    Views:
    712
    Jim Langston
    Sep 22, 2005
  3. SuperZE
    Replies:
    5
    Views:
    348
    SuperZE
    Oct 6, 2008
  4. Michael

    Child user control accessing parent properties

    Michael, Jul 13, 2004, in forum: ASP .Net Building Controls
    Replies:
    9
    Views:
    489
    John Saunders
    Jul 30, 2004
  5. whidbeywave
    Replies:
    3
    Views:
    204
    whidbeywave
    Jan 16, 2006
Loading...

Share This Page