Sorry, this is more likely a Python question than it is a Pythonista question. But I have searched else where and can not find the answer. It also could be particular to Pythonista, not sure.
What I would like to do is , add a function to a ui Element such as a ui.Button. Then from inside that function at runtime determine the calling classes instance. so self for short.
In the sample code below, I know, I could pass the instance object as a param. Just not ideal....
I think the answer must be in the inspect module. I have tried with stack(), current(), outerframes(), I just can't seem to figure it out.
If you do have a solution to share that would be great, but could you please also comment on how robust you think the solution is.
import ui
def test_func(msg):
'''
i would like to get a reference to the caller here
being the btn.
i am pretty sure the answer lies with the inspect module,
but i cant figure it out.
MAYBE, its even easier than that... well that would be great
'''
print msg
btn = ui.Button()
btn.msg = test_func
btn.msg('I am on to something...')