is decorator the right thing to use?
Dmitry S. Makovey
dmitry at makovey.net
Wed Sep 24 23:35:06 EDT 2008
showellshowell at gmail.com wrote:
> Your code below is very abstract, so it's kind of hard to figure out
> what problem you're trying to solve, but it seems to me that you're
> using the B proxy class to decorate the A target class, which means
> you want one of these options:
Sorry for unclarities in original post. Basically A aggregates object of
class B (example with no decorators and again it's oversimplified):
class A:
b=None
def __init__(self,b):
self.b=b
def amethod(self,a):
print "A::amethod ", a
def bmethod(self,a):
print "A::bmethod ",a
return self.b.bmethod(a)
def bmethod2(self,a,z):
print "A::bmethod2 ",a,z
return self.b.bmethod2(a,z)
class B:
def __init__(self):
self.val=a
def bmethod(self,a):
print "B::bmethod ",a
def bmethod2(self,a,z):
print "B::bmethod2 ",a,z
b=B()
a=A(b)
a.bmethod('foo')
a.bmethod2('bar','baz')
In my real-life case A is a proxy to B, C and D instances/objects, not just
one. If you look at above code - whenever I write new method in either B, C
or D I have to modify A, or even when I modify signature (say, add
parameter x to bmethod) in B, C or D I have to make sure A is synchronized.
I was hoping to use decorator to do it automatically for me. Since the
resulting code is virtually all the same for all those proxy methods it
seems to be a good place for automation. Or am I wrong assuming that?
(since it is my first time using decorators I honestly don't know)
Abovementioned code ilustrates what I am doing right now. My original post
is an attempt to make things more automated/foolproof.
More information about the Python-list
mailing list