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?
This is impossible in general.
However, if you’re creating the
Buttonclass, you can pass a special sentinel value that means “yourself”. For example:Then:
Picking an appropriate sentinel can be tricky—obvious choices like
None,0, or''may be legitimate values, and even tricky things you come up with may turn out to be useful arguments during debugging. Makingyourselfa class variable, or a global within the module, means that if you ever do need to redefine the sentinel, you only need to change it in one place, instead of everywhere you use it.See http://bytes.com/topic/python/answers/555169-sentinel-values-special-cases for a brief discussion on picking an appropriate sentinel value. There’s another blog out there with more information, but I haven’t found it in a quick search… Anyway, here are some quick ideas:
Noneis always the best answer if it works.objectclass (object()).func_codeor whatever).Ellipsis(ortype(Ellipsis), which is a type namedellipsis, but that name isn’t accessible) is almost always safe, because it’s only used in__getitem__and friends (and possibly in definingsliceobjects to pass to them).func_codemember of the__init__function.