I've got a button class that you can instantiate like so:
engine.createElement((0, 0), Button(code=print, args=("Stuff!",)))
engine.createElement((0, 0), Button(code=engine.killElement, args=(self,)))
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]
engine.createElement((0, 0), Button(code=engine.killElement, args=(Button.yourself,)))
Picking an appropriate sentinel can be tricky—obvious choices like
'' 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:
Noneis always the best answer if it works.
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
sliceobjects to pass to them).
func_codemember of the