Name McChange Name McChange - 1 month ago 4
Python Question

Give a class its own `self` at instantiation time?

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
refer to the
variable upon click, but what if I wanted to use the string
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]


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 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:

  1. None is always the best answer if it works.
  2. Define an empty class to be the sentinel. Either the class object, or any instance of the class object, can be used.
  3. Create a global instance of the object class (object()).
  4. Define an empty function and use it (or its func_code or whatever).
  5. 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).
  6. If there's a type that could not possibly be a valid value, and you've already got instances around, use one of those—e.g., the func_code member of the __init__ function.