Re: python classes and signal connects



Hi Simon,

On Mo, 2012-10-01 at 13:44 -0700, Simon Feltman wrote:
> It would help to give some context as to what you are trying to
> accomplish with having a class. In the example the TestSignals class
> is superfluous "t = TestSignals()" could be replaced with the contents
> of __init__ and the callback could be moved out as a standalone
> function. But I gather what you are actually trying to do is more
> complex than this?

I'm trying to port d-feet to pygi and gdbus [1]. The TestSignals() class
is just an example.

> The specific issue you are seeing is because the callback connected to
> the signal is a bound method: "self.button_clicked_cb". A bound method
> will keep a reference to the instance of its class TestSignals and the
> function. This can be observed as follows:
> 
> test = TestSignals()
> test.button_clicked_cb.__self__ == test  # True
> 
> If button_clicked_cb does not access any state on the TestSignals
> instance, than by all means it should be moved outside of the class or
> made into a staticmethod or classmethod.

The method button_clicked_cb() needs access to the class instance.
So @staticmethod does not help because then I have to provide the instance as
user_data like this:

def __init__(self):
        self.button = Gtk.Button("My Button")
        self.button.connect("clicked", self.button_clicked_cb, self)
    
    @staticmethod
    def button_clicked_cb(button, user_data):
        print "button clicked"


And then __del__ is never called, too.


Cheers,

Tom


[1]
https://gitorious.org/d-feet/d-feet/blobs/pygi/dfeet/introspection.py





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]