Cast into custom type

Henning Bredel henning.bredel at gmx.de
Tue Nov 3 13:07:01 CET 2009


Diez, Gabriel, Steven,

thanks for your answers. I'll mainly give response in this posting, so 
I hope not repeating myself.

On Tue, 03 Nov 2009 10:18:29 +0000, Steven D'Aprano wrote:

> On Tue, 03 Nov 2009 09:41:37 +0000, Henning Bredel wrote:
> 
>> Now I'd like to call methods like `initialize(parent)' when the user
>> chooses to use a plugin. As described in the blog mentioned above, I
>> only have access to the general type called `PluginMount' (holding all
>> the actual plugin instances).
>> 
>> I tried to define "abstract" methods in PluginMount type raising a
>> `NotImplementedError' but it seems, there is no late binding (similar
>> to Java), so the right method would be called.
> 
> You need to give some actual examples of what you are trying to do, and
> what you are expecting to happen. How is initialized() being called?

Example: Assume a framework which offers common functionality for a plugin
or a module a user can choose at the beginning. The framework does not
know the concrete type of the plugin so it is possible to extend it by
implementing a well known interface or abstract class.

The framework reads the plugin directory, loads each module and creates
buttons for each plugin with a callback method for initializing. To use
common functionality of the framework, initialization method takes it as
the parent parameter.

I think this listing makes the most sense to you:

  # initialize all plugins
  self._plugin_modules = _load_plugins() # imp loading here
  LOGGER.debug(ActionProvider.plugins) # print what was loaded
  for plugin in ActionProvider.plugins: # create button for each
      app_button = gtk.Button(plugin.title)
      LOGGER.debug('Title of plugin: %s' % plugin.title)
      app_button.connect("clicked", 
                         plugin.initialize(plugin, self), 
                         None)
      self.set_canvas(app_button)
      app_button.show()

>>   TypeError: unbound method initialize() must be called with GeoCache
>>   instance as first argument (got PluginMount instance instead)
> 
> Sounds like you are calling initialize on the class instead of on an
> instance. I'm *guessing* that you are doing something like this:

Oh, I didn't expext PluginMount to hold only classes :/.

When calling `LOGGER.debug('Title of plugin: %s' % plugin.title)' the
global (not class attribute) `title' attribute is printed out though.
Probably some misinterpretation, too ..?!

> (1) Change the for-loop to:
> 
> for cls in plugin_manager.plugins:
>     cls().initialize(plugin_Manager)

Good guess .. but calling it in that way another error says

  app_button.connect("clicked", plugin().initialize(self), None)
  TypeError: __init__() takes exactly 2 arguments (1 given)

But also

  app_button.connect("clicked", plugin().initialize(plugin, self), None)
  TypeError: __init__() takes exactly 2 arguments (1 given)

That is strange, because neither initialize() nor __init__ of the
plugin is called .. only a logging statement shall print a msg.

I hope it got a bit clearer what I am intended to do.

Thanks for your help.

  Henning




More information about the Python-list mailing list