is decorator the right thing to use?

George Sakkis george.sakkis at gmail.com
Sun Sep 28 06:19:45 EDT 2008


On Sep 27, 11:38 pm, "Dmitry S. Makovey" <dmi... at makovey.net> wrote:
> George Sakkis wrote:
> > It's funny how often you come with a better solution a few moments
> > after htting send! The snippet above can (ab)use the decorator syntax
> > so that it becomes:
>
> > class A(Proxy):
>
> >     @ProxyMethod
> >     def bmethod(self):
> >         return self.b1
>
> >     @ProxyMethod
> >     def bmethod2(self):
> >         return self.b2
>
> That is outstanding!

FYI, in case you missed it the final version doesn't need a Proxy base
class, just inherit from object. Also lowercased ProxyMethod to look
similar to staticmethod/classmethod:

class A(object):

    def __init__(self, b1, b2):
        self.b1 = b1
        self.b2 = b2

    @proxymethod
    def bmethod(self):
        return self.b1

    @proxymethod
    def bmethod2(self):
        return self.b2

George



More information about the Python-list mailing list