Making a decorator a staticmethod

Carl Banks pavlovevidence at gmail.com
Fri Jan 9 05:40:37 CET 2009


On Jan 8, 1:57 pm, Jonathan Gardner <jgard... at jonathangardner.net>
wrote:
> (Aside: I really can't think of any reason to use staticmethods in
> Python other than to organize functions into namespaces, and even
> then, that's what modules are for, right?)

In a certain widget toolkit (which I won't mention by name but it
begins with G, ends with K, and the middle rhymes with "be") sometimes
when subclassing a widget and setting up methods of the class to be
event handlers, you want to use staticmethods, because the toolkit
passes the widget as the first argument.  Since this is a subclass of
the widget, the first argument ends up being the object itself, so we
end up with odd-looking definitions like this:

@staticmethod
def on_mouse_button_down(self,event):

If it wasn't for staticmethods, you'd have to define it like this:

def on_mouse_button_down(self,widget,event):

widget and self would always be the same object, and that's just a
waste.



Carl Banks



More information about the Python-list mailing list