Dmitry S. Makovey schrieb:
Dmitry S. Makovey wrote:
In my real-life case A is a proxy to B, C and D instances/objects, not
just one.

forgot to mention that above would mean that I need to have more than one
decorator function like AproxyB, AproxyC and AproxyD or make Aproxy smarter
about which property of A has instance of which class etc.
Unless I'm totally "out for lunch" and there are better ways of implementing
this (other than copy-pasting stuff whenever anything in B, C or D
changes).

__getattr__?

class Proxy(object):


    def __init__(self, delegate):
        self._delegate = delegate


    def __getattr__(self, attr):
        v = getattr(self._delegate, attr)
        if callable(v):
           class CallInterceptor(object):
                 def __init__(self, f):
                     self._f = f

                 def __call__(self, *args, **kwargs):
print "Called " + str(self._f) + " with " + str(args) + str(kwargs)
                     return self._f(*args, **kwargs)
           return CallInterceptor(v)
        return v


Decorators have *nothing* to do with this. They are syntactic sugar for


def foo(...):
    ...

foo = a_decorator(foo)

Nothing less, nothing more.

Diez
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to