Descriptor/Decorator challenge

Raymond Hettinger python at rcn.com
Mon Mar 5 02:31:30 EST 2007


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




More information about the Python-list mailing list