Local methods

In the file local.py (download), a localmethod decorator is implemented. When decorating a method, it makes it local to its class. I.e. only other methods defined in this class can call it (a bit like a private method).

A simple example

>>> class A(Object):
...     @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()
A.m
>>> m.bm()
B.m
>>> B.am(m)
A.m
>>> B.bm(m)
B.m
>>> m.m()
Traceback ... __main__.LocalMethodError: method 'm' is local to 'B'
>>>

Another example

>>> @function # This is needed so that __callerstack__ can be updated
... def fun(x):
...     x.f("from fun")
... 
>>> class Foo(Object):
...     @localmethod
...     def f(self, x):
...         print "Foo.f", x
...     def g(self):
...         self.f("from Foo.g")
...     def h(self):
...         fun(self)
... 
>>> class Bar(Foo):
...     def g(self):
...         super(Bar, self).g()
...     def h(self):
...         self.f("from Bar.g")
... 
>>> foo=Foo()
>>> bar=Bar()
>>> foo.g() 
Foo.f from Foo.g
>>> foo.h()
Traceback ... __main__.LocalMethodError: method 'f' is local to 'Foo'
>>> bar.g()
Foo.f from Foo.g
>>> bar.h()
Traceback ... __main__.LocalMethodError: method 'f' is local to 'Foo'
>>>
Last updated on Tue Oct 28 16:16:12 2008
arno AT marooned.org.uk