In a function, how to get the caller object ?

popov google at evpopov.com
Wed Dec 3 15:56:06 CET 2003


Look at this code:

class C:
    def func():
        <caller_of_func>.my_attrib = 1
    func = staticmethod(func)        

C.func()

c = C()
c.func()

In func, I would like to get C for the first call, and c for the
second one, to be able to add a new attribute to this object (so that
would add the attribute either to the class object or to the
instance). How can I do this ?
I tried to give a look to the inspect module with no luck.




More information about the Python-list mailing list