is decorator the right thing to use?

Dmitry S. Makovey dmitry at athabascau.ca
Fri Sep 26 19:30:52 EDT 2008


Aaron "Castironpi" Brady wrote:
> That prohibits using a descriptor in the proxied classes, or at least
> the proxied functions, since you break descriptor protocol and only
> call __get__ once.  Better to cache and get by name.  It's only slower
> by the normal amount, and technically saves space, strings vs.
> instancemethod objects (except for really, really long function names).

that is an interesting point since I didn't think about having descriptors
in proxied classes. my reworked code clearly breaks when descriptors are
thrown at it. It will break with methods in proxied objects that are
implemented as objects too. Now I adjusted constructor a bit to account for
that (I just can't figure out case when I'll be proxying descriptors unless
they return function but than I don't see benefit in using descriptor for
that, probably because I haven't used them much).

class ProxyMethod(object):
    def __init__(self,ob_name,meth):
        self.ob_name=ob_name
        if not hasattr(meth,'im_class'):
            if hasattr(meth,'__call__'):
                self.meth=getattr(meth,'__call__')
            else:
                raise ValueError("Method should be either a class method or
a callable class")
        else:
            self.meth=meth





More information about the Python-list mailing list