How do I assign a method decorator from a class property in python?
<p>So, I know I can do this without a problem:</p>

<pre><code>button = widget.libray.Button()

class X():
  @button.on_click
  def click(self, event):
    ...
</code></pre>

<p>Without a problem.</p>

<p>...but it's irritating, because now 'button' is outside the class scope; I'd like this to be a class property. How do I use the @blah decorate syntax for a class member?</p>

<p>I can certainly not use the decorator at all, like this:</p>

<pre><code>class UiDemo(cocos.cocosnode.CocosNode):

  def on_enter(self):
    self.b = Button(text='Hello World')
    self.bt = b.event(self.bt) # &lt;--- !!! Instead of decorator!
    vbox = VBox(elements=[self.b])
    self.dialog = Dialog(title='My Dialog', x=100, y=100, content=vbox, width=200, height=100)
    cocos.director.director.window.push_handlers(self.dialog)

  # @self.b.event doesn't work here, obviously.
  def bt(self, button):
     print('Button pressed: %s' % button)

  def on_exit(self):
    cocos.director.director.window.remove_handlers(self.dialog)

  def draw(self):
    self.dialog.on_draw()

class Client:
  def run(self):
    cocos.director.director.init()
    test = UiDemo()
    main_scene = cocos.scene.Scene(test)
    cocos.director.director.run (main_scene)
</code></pre>

<p>I mean, I see the problem; the method is defined on the class when the class is defined (I guess), so even if you create the widget in the <strong>init</strong>() step, it doesn't exist at the point where the class definition is parsed, and so it can't be used as a decorator at that time.</p>

<p>What I'm wondering is if there's some way of working around this?</p>

<p>Surely there's a fair number of cases where you want your decorator property to reference to an instance of something that's attached to the class?</p>

<p>(as you can see from my example above, this <em>is not</em> just a "what if" situation; this is the second time this week I've ended up using the verbose self.func = self.instance.dec(self.func) form to do this)</p>

<p>Any suggestions? </p>

<p>(...obviously, other than, use libraries that don't do that because it's dumb; that's not really a solution. :P)</p>
