i want to write a decorator that enables methods of classes to become visible to other parties; the problem i am describing is, however, independent of that detail. the code will look roughly like this:
def CLASS_WHERE_METHOD_IS_DEFINED( method ):???def foobar( method ):print( CLASS_WHERE_METHOD_IS_DEFINED( method ) )class X:@foobardef f( self, x ):return x ** 2
my problem here is that the very moment that the decorator, foobar()
, gets to see the method, it is not yet callable; instead, it gets to see an unbound version of it. maybe this can be resolved by using another decorator on the class that will take care of whatever has to be done to the bound method. the next thing i will try to do is to simply earmark the decorated method with an attribute when it goes through the decorator, and then use a class decorator or a metaclass to do the postprocessing. if i get that to work, then i do not have to solve this riddle, which still puzzles me:
can anyone, in the above code, fill out meaningful lines under CLASS_WHERE_METHOD_IS_DEFINED
so that the decorator can actually print out the class where f
is defined, the moment it gets defined? or is that possibility precluded in python 3?