Do python class-methods have a method/member themselves, which indicates the class, they belong to?
For example ...:
# a simple global function dummy WITHOUT any class membership def global_function(): print('global_function') # a simple method dummy WITH a membership in a class class Clazz: def method(): print('Clazz.method') global_function() # prints "global_function" Clazz.method() # prints "Clazz.method" # until here, everything should be clear # define a simple replacement def xxx(): print('xxx') # replaces a certain function OR method with the xxx-function above def replace_with_xxx(func, clazz = None): if clazz: setattr(clazz, func.__name__, xxx) else: func.__globals__[func.__name__] = xxx # make all methods/functions print "xxx" replace_with_xxx(global_function) replace_with_xxx(Clazz.method, Clazz) # works great: global_function() # prints "xxx" Clazz.method() # prints "xxx" # OK, everything fine! # But I would like to write something like: replace_with_xxx(Clazz.method) # instead of replace_with_xxx(Clazz.method, Clazz) # note: no second parameter Clazz! Now my question is: How is it possible, to get all method/function calls print "xxx", WITHOUT the "clazz = None" argument in the replace_with_xxx function???
Is there something possible like:
def replace_with_xxx(func): # before it was: (func, clazz = None) if func.has_class(): # something possible like this??? setattr(func.get_class(), func.__name__, xxx) # and this ??? else: func.__globals__[func.__name__] = xxx Thank you very much for reading. I hope, i could make it a little bit clear, what i want. Have a nice day! :)