Descriptor/Decorator challenge

George Sakkis george.sakkis at gmail.com
Tue Mar 6 04:26:33 CET 2007


On Mar 5, 2:31 am, "Raymond Hettinger" <pyt... at rcn.com> wrote:

> I had an idea but no time to think it through.
> Perhaps the under-under name mangling trick
> can be replaced (in Py3.0) with a suitably designed decorator.
> Your challenge is to write the decorator.
> Any trick in the book (metaclasses, descriptors, etc) is fair game.
>
> Raymond
>
> -------- how we currently localize method access with name mangling
> ------
> class A:
>     def __m(self):
>         print 'A.__m'
>     def am(self):
>         self.__m()
>
> class B(A):
>     def __m(self):
>         print 'B.__m'
>     def bm(self):
>         self.__m()
>
> m = B()
> m.am()     # prints 'A.__m'
> m.bm()     # prints 'B.__m'
>
> -------- how I would like to localize method access with a decorator
> ------
> class A:
>     @localmethod
>     def m(self):
>         print 'A.m'
>     def am(self):
>         self.m()
>
> class B(A):
>     @localmethod
>     def m(self):
>         print 'B.m'
>     def bm(self):
>         self.m()
>
> m = B()
> m.am()     # prints 'A.m'
> m.bm()     # prints 'B.m'
>
> ---------------------
>
> P.S. Here's a link to the descriptor how-to:
>    http://users.rcn.com/python/download/Descriptor.htm

What would the semantics be if m is decorated as local only in A or
only in B ?

George




More information about the Python-list mailing list