Cast into custom type

Steven D'Aprano steve at
Tue Nov 3 11:18:29 CET 2009

On Tue, 03 Nov 2009 09:41:37 +0000, Henning Bredel wrote:

> Hi,
> I created a plugin mechanism for my application orientating at the
> mechanism described by Martin Alchy in

Regarding your subject line, Python doesn't have casts. A very small 
number of numeric types are automatically coerced as needed, for 
everything else you need an explicit conversion between objects of one 
type to different objects of another type.

> 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?

> Only the message
>   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:

plugin_manager = PluginMount()
# ...
# create plugins, which are registered in the plugin_manager
# ...
# Now initialize them:
for cls in plugin_manager.plugins:

If my guess is correct, you can fix this two ways:

(1) Change the for-loop to:

for cls in plugin_manager.plugins:

although it isn't clear to me what you should do with the plugin 
instances after you've created them.

(2) Change the initialize method to a classmethod:

def initialize(cls, parent):

> `GeoCache' would be the plugin type. What is strange, is the fact, that
> when asking what instances are hold by PluginMount
>    [<class 'geocacheplugin.GeoCache'>]
> is listed. So it seems, that no late binding is applied when calling the
> `initialize(self, parent)' method.

What do you mean by late binding, and what makes you think it applies (or 
should apply) to calling the initialize method?

> I'm quite new using Python, so this might be a quite basic question
> caused by some misunderstandings in general. Feel free to point me to
> appropriate site to solve my problem.

We have to understand your problem first.


More information about the Python-list mailing list