Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Accessing object parent properties

Reply
Thread Tools

Re: Accessing object parent properties

 
 
Ryan Forsythe
Guest
Posts: n/a
 
      05-23-2006
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.


 
Reply With Quote
 
 
 
 
bruno de chez modulix en face
Guest
Posts: n/a
 
      05-26-2006
> 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).

 
Reply With Quote
 
 
 
 
bruno de chez modulix en face
Guest
Posts: n/a
 
      05-26-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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
Class properties and object properties SuperZE Python 5 10-06-2008 02:14 PM
how to resolve for permission denied when accessing any object using parent. using javascript? whidbeywave Javascript 3 01-16-2006 04:25 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Child user control accessing parent properties Michael ASP .Net Building Controls 9 07-30-2004 09:46 PM



Advertisments