Plugins: SpyderPluginWidget - __init__() vs register_plugin()

44 views
Skip to first unread message

anatoly techtonik

unread,
Dec 3, 2011, 4:27:51 AM12/3/11
to spyd...@googlegroups.com
What is the purpose of register_plugin() if Spyder plugins need to include __init__() function anyway for calling parent constructors? I can't see any significant differences in what should and should not be done in either of these obligatory plugin functions.

anatoly techtonik

unread,
Dec 3, 2011, 5:30:27 AM12/3/11
to spyd...@googlegroups.com
I can understand the case when plugin doesn't inherit from SpyderPluginWidget, but merely implements its interface, but it still will be a class and class constructor is called anyway.

Pierre Raybaut

unread,
Dec 3, 2011, 1:28:21 PM12/3/11
to spyd...@googlegroups.com
There are a couple of reasons why I wrote the 'register_plugin' in Spyder's plugins interface:
  * the 'register_plugin' method is doing more than a constructor should do: it registers the plugin to Spyder's main window (connecting signals, ...) -- I tend to avoid constructor methods that do everything: it's less readable
  * separating this method from the constructor also has the advantage of making it easier to change the code structure in the future
  * there is another reason/motivation for this, but I can't remember it -- my guess is that was at some point essential to create this method but the situation changed and now it's not as needed as it was (except for the reasons above)

anatoly techtonik

unread,
Dec 13, 2011, 5:25:10 AM12/13/11
to spyd...@googlegroups.com
Following this philosophy, how about removing `parent` parameter to plugin constructor and pass it to register_plugin() instead? In this case plugin constructor will just provide a placeholder for storing reference to parent Spyder window, which is passed to register_plugin() that does all other complicated logic like calling SpyderPluginMixin.initilialize_plugin() etc.

The benefit of that:
 - discourage users from doing too much stuff in __init__() (obviously they can't do much without a reference to Spyder's main window)
 - add ability to control if plugin failed to register by checking return code of register_plugin() function

Reply all
Reply to author
Forward
0 new messages