GNOME Bugzilla – Bug 588762
do_* methods are not bound to class instances to which they refer
Last modified: 2010-12-23 21:36:12 UTC
Please describe the problem: (apparently all) do_* methods are not bound to class instances to which they (should) refer. Steps to reproduce: 1. ipython 2. import gtk 3. r = gtk.CellRendererText() 4. r.do_get_size(gtk.Window()) Actual results: TypeError Traceback (most recent call last) /home/pietro/<ipython console> in <module>() TypeError: GtkCellRenderer.do_get_size() argument 1 must be gtk.CellRenderer, not gtk.Window Expected results: Out[5]: (0, 0, 4, 17) Does this happen every time? Yes Other information: The following works (apparently across all classes and do_* methods): In [6]: r.do_get_size(r, gtk.Window()) Out[6]: (0, 0, 4, 17)
This is not a bug, the do_foo_bar_* methods take the class itself as first parameter, why do you think this is a bug?
With the following code: import gtk, gobject class CellRendererBig(gtk.CellRendererText): def do_get_size(self, widget, cell_area): print "self is", self return gtk.CellRendererText.do_get_size(self, widget, cell_area) gobject.type_register(CellRendererBig) w=gtk.Window() r=CellRendererBig() print "r is", r t=gtk.TreeView() c=gtk.TreeViewColumn("title", r) c.add_attribute(r, 'text', 0) t.append_column(c) m=gtk.ListStore(str) t.set_model(m) m.insert(1000, ["long text to check get_size still works"]) w.add(t) w.show_all() gtk.main() we see that (as one would expect) "self" is exactly "r", not its class... so I don't get what you mean.
OK, I'll say it in another way: r.do_get_size(r, gtk.Window()) doesn't pass "the class itself" as first parameter, it passes the _instance_. So 1) again, I don't see what you mean in your answer 2) it seems to me really not pythonic that a method on an object must be _called_ with the object also as parameter. but then, it's certainly not an unsurmountable problem, so if you say _this_ is the right way because of some reason I'm missing, the bug can be certainly closed.
Created attachment 176974 [details] virtual_methods example
I'd agree with Gian Mario that this is not a bug. I do see why the behavior described above would take people by surprise. Normal methods, when called on an instance, implicitly fill in the first parameter (the instance of the object the method was called on, also known as "self": >>> class Test(object): ... def hello(self, param): ... print self, param ... >>> test = Test() >>> test.hello('sometext') <__main__.Test object at 0x011BA330> sometext >>> Virtual methods (do_*) are however not really intended to be used outside (sub)class definitions. For the original question, the do_get_size virtual method is the implementation behind the get_size method [1]. The attached virtual_methods.py shows an example of how the get_size method relates to the do_get_size virtual method. [1] http://library.gnome.org/devel/pygtk/2.22/class-gtkcellrenderer.html#method-gtkcellrenderer--get-size