Hope the title isn’t to confusing wasn’t sure how I should put it. I wonder if it’s possible for the base class to know which method of the derived class called one of it’s methods.
Example:
class Controller(object):
def __init__(self):
self.output = {}
def output(self, s):
method_that_called_me = #is it possible?
self.output[method_that_called_me] = s
class Public(Controller):
def about_us(self):
self.output('Damn good coffee!')
def contact(self):
self.output('contact me')
So is it possible for the output method to know which method from the Public class called it?
There is a somewhat magical way to do what you are looking for using introspection on the call stack. But that isn’t portable since not all implementations of Python have the necessary functions. It’s probably not a good design decision to use introspection either.
Better, I think, to be explicit:
PS. Note that you have
self.outputas both adictand amethod. I’ve altered it soself._outputis adict, andself.outputis the method.PPS. Just to show you what I was referring to by magical introspection: