472,330 Members | 1,233 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,330 software developers and data experts.

Transfer undefined class methods to attribute's method.

Hello,

Maybe I was a little too detailed in my previous post [same title]. I can
boil down my problem to this: say I have a class A that I encapsulate with
a class Proxy. Now I just want to override and add some functionality (see
my other post why). All functionality not defined in the Proxy class should
be delegated (I can't use inheritance, see other post). It should be
possible to achieve this using Python's great introspection possibilities,
but I can't find out how. Any help would be really appreciated!

TIA, Maarten

Example (class A and Proxy):

class A:
def __init__(self):
pass

def methodA(self):
pass

def methodB(self):
pass

def methodC(self):
pass

class Proxy:
def __init__(self):
self.a = A()

# maybe scan the methods in A and not in this class?????
# setup a hook for undefined methods?

def methodA(self):
# what I DON'T want:
return self.a.methodA()
# and this for every method...
# maybe something like this?
def process_unknownmethod(self, method, args):
return self.a.method(args)

P = Proxy()
P.methodA()
P.methodC()

output:
Traceback (most recent call last):
File "group.py", line 36, in ?
P.methodC()

--
================================================== =================
Maarten van Reeuwijk Thermal and Fluids Sciences
Phd student dept. of Multiscale Physics
www.ws.tn.tudelft.nl Delft University of Technology
Jul 18 '05 #1
4 1793
Maarten van Reeuwijk wrote:
Hello,

Maybe I was a little too detailed in my previous post [same title]. I can
boil down my problem to this: say I have a class A that I encapsulate with
a class Proxy. Now I just want to override and add some functionality (see
my other post why). All functionality not defined in the Proxy class should
be delegated (I can't use inheritance, see other post). It should be
possible to achieve this using Python's great introspection possibilities,
but I can't find out how. Any help would be really appreciated!

TIA, Maarten

Example (class A and Proxy):

class A:
def __init__(self):
pass

def methodA(self):
pass

def methodB(self):
pass

def methodC(self):
pass

class Proxy:
def __init__(self):
self.a = A()

# maybe scan the methods in A and not in this class?????
# setup a hook for undefined methods?

def methodA(self):
# what I DON'T want:
return self.a.methodA()
# and this for every method...
# maybe something like this?
def process_unknownmethod(self, method, args):
return self.a.method(args)

P = Proxy()
P.methodA()
P.methodC()

output:
Traceback (most recent call last):
File "group.py", line 36, in ?
P.methodC()


This might help (almost untested):

class Proxy(object):
def __init__(self, obj):
self.obj_ = obj

def __getattr__(self, name):
return getattr(self.obj_, name)

class Foo(object):
def methodA(self):
return 'Foo.methodA'

def methodB(self):
return 'Foo.methodB'

foo = Foo()
p = Proxy(foo)

print p.methodA()
print p.methodB()

regards,
anton.
Jul 18 '05 #2
> Maybe I was a little too detailed in my previous post [same title]. I can
boil down my problem to this: say I have a class A that I encapsulate with
a class Proxy. Now I just want to override and add some functionality (see
my other post why). All functionality not defined in the Proxy class
should be delegated (I can't use inheritance, see other post). It should
be possible to achieve this using Python's great introspection
possibilities, but I can't find out how. Any help would be really
appreciated!


Why don't you just scan the underlying object for all methods it has (using
the objects __dict__, filtering with type method) and add these methods on
your proxy when there exists no method of the same name (using the proyies
dict)? That would eliminate the need of a general-purpose method call
interception and do what you want.

I'm a little bit too lazy right now create a working example, but I think
you should be able to come up with your own in no time - if not, ask again
(and someone more enlightened might answer, or I get down on my a** and
write something... :))

--
Regards,

Diez B. Roggisch
Jul 18 '05 #3
Maarten van Reeuwijk wrote:
Hello,

Maybe I was a little too detailed in my previous post [same title]. I can
boil down my problem to this: say I have a class A that I encapsulate with
a class Proxy. Now I just want to override and add some functionality (see
my other post why). All functionality not defined in the Proxy class
should be delegated (I can't use inheritance, see other post). It should
be possible to achieve this using Python's great introspection
possibilities, but I can't find out how. Any help would be really
appreciated!

TIA, Maarten

Example (class A and Proxy):

class A:
def __init__(self):
pass

def methodA(self):
pass

def methodB(self):
pass

def methodC(self):
pass

class Proxy:
def __init__(self):
self.a = A()

# maybe scan the methods in A and not in this class?????
# setup a hook for undefined methods?

def methodA(self):
# what I DON'T want:
return self.a.methodA()
# and this for every method...
# maybe something like this?
def process_unknownmethod(self, method, args):
return self.a.method(args)

P = Proxy()
P.methodA()
P.methodC()

output:
Traceback (most recent call last):
File "group.py", line 36, in ?
P.methodC()


The __getattr__() method could be helpful for your problem. It's calld for
every missing attribute - not just methods. If you want to set attributes,
there's a corresponding __setattr__() method which is called for *every*
attribute.

Adopting your example:

class A:
def methodA(self):
print "original A"

def methodB(self):
print "original B"
class Proxy:
def __init__(self, wrapped):
self._wrapped = wrapped

def methodA(self):
print "replacement A, internally calling",
return self._wrapped.methodA()

def __getattr__(self, name):
return getattr(self._wrapped, name)

a = A()
p = Proxy(a)
p.methodA()
p.methodB()
Peter
Jul 18 '05 #4
That does the trick!

Thanks guys,

Maarten
--
================================================== =================
Maarten van Reeuwijk Thermal and Fluids Sciences
Phd student dept. of Multiscale Physics
www.ws.tn.tudelft.nl Delft University of Technology
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be...
0
by: Maarten van Reeuwijk | last post by:
I am constructing a "placeholder array" to mimick Numeric arrays. I have a 3D regular structured domain, so I can describe the axis with 3 1D arrays...
3
by: Robert | last post by:
Python doesn't know the class of a method when container not direct class attribute: >>> class X: .... def f():pass .... g=f .... l= .......
4
by: Terry Olsen | last post by:
Since both methods seem to produce the same results, in which cases would you prefer one over the other? The only thing I would think is using...
2
by: Vivek Ragunathan | last post by:
Hi Are the members in a static class in C# class synchronized for multiple thread access. If yes, are all static members in a C# class auto...
22
by: Saul | last post by:
I have a set of radio buttons that are created dynamically, after rendered I try loop thru this set by getting the length of the set, but I keep...
4
by: Pedro Werneck | last post by:
Hi all I noticed something strange here while explaining decorators to someone. Not any real use code, but I think it's worth mentioning. ...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract...
4
by: Travis | last post by:
Is it considered good practice to call a mutator when inside the same class or modify the attribute directly? So if there's a public method...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.