Build a class from an instance of a superclass

So, you have a function, say Gtk.Builder.get_object(), that returns some widget. In our case, a Gtk.Window().

I have a subclass Gtk.Window()that adds some signal handlers.

class Window(Gtk.Window):

Is it possible to use the widget returned Gtk.Builder.get_object()to build Window()? I think he should use __new__()or something else, but I can’t figure it out.

+1
source share
1 answer

I think use __new__is exactly what you want to do. If you can set the attribute of an __class__instance of a superclass that you fall into a subclass, you must be set.

Here is what I think you need:

class Window(Gtk.Window):
    def __new__(cls, *args, **kwargs):
        self = Gtk.Builder.get_object()
        self.__class__ = cls
        return self

Python , , __new__, ( __class__), __init__ , .

+1

All Articles