Python: Give a class its own `self` at instantiation time
- by SuperDisk
I've got a button class that you can instantiate like so:
engine.createElement((0, 0), Button(code=print, args=("Stuff!",)))
And when it is clicked it will print "Stuff!". However, I need the button to destroy itself whenever it is clicked. Something like this:
engine.createElement((0, 0), Button(code=engine.killElement, args=(self,)))
However, that would just kill the caller, because self refers to the caller at that moment. What I need to do is give the class its own 'self' in advance...
I thought of just making the string 'self' refer to the self variable upon click, but what if I wanted to use the string 'self' in the arguments?
What is the way to do this? Is my architecture all wrong or something?
Thanks.