Locking some methods from certain clients

Adrian Eyre a.eyre at optichrome.com
Fri Oct 1 08:41:28 EDT 1999


> 	I want to lock the accesse of some method of an object from other
> objects using it. But on a class by class basis. What I want is the
> possibility to know the class-name of a caller and if this caller is on
> my access list let it use my method. Something like this:
> [snip]

Try something like this:

import sys

class A:
    def callee(self):
        try:
            raise "This is a bit of a hack really"
        except:
            print "I was called by class " +
sys.exc_info()[2].tb_frame.f_back.f_locals["self"].__class__.__name__

    def caller(self):
        self.callee()

class B(A):
    def caller(self):
        self.callee()


def test():
    a = A()
    b = B()
    a.caller()
    b.caller()

if __name__ == "__main__":
    test()


Be careful though. This makes the assumptions:
1. You are only going to call A.callee from an instance method
2. The instance method's first parameter is called "self"

--------------------------------------------
Adrian Eyre <mailto:a.eyre at optichrome.com>
Optichrome Computer Solutions Ltd
Maybury Road, Woking, Surrey, GU21 5HX, UK
Tel: +44 1483 740 233  Fax: +44 1483 760 644
http://www.optichrome.com
--------------------------------------------





More information about the Python-list mailing list