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 Button
class, you can pass a special sentinel value that means "yourself". For example:
class Button(object):
yourself = 'yourself'
def __init__(self, code, args):
self.code = code
self.args = [self if arg is yourself else arg for arg in args]
Then:
engine.createElement((0, 0), Button(code=engine.killElement, args=(Button.yourself,)))
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. Making yourself
a 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:
None
is always the best answer if it works. object
class (object()
).func_code
or whatever).Ellipsis
(or type(Ellipsis)
, which is a type named ellipsis
, but that name isn't accessible) is almost always safe, because it's only used in __getitem__
and friends (and possibly in defining slice
objects to pass to them).func_code
member of the __init__
function.