Automatically loading and initialising objects from a plugins directory
Dave Challis
dsc at ecs.soton.ac.uk
Fri Jul 25 07:27:04 EDT 2008
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Fredrik Lundh wrote:
> so now you're no longer supporting mixins and multiple-level
> inheritance? why not just tweak Diez' example a little:
>
> for name in dir(plugin):
>
> thing = getattr(plugin, name)
>
> # make sure this is a plugin
> try:
> if thing is Plugin:
> continue
> if not issubclass(thing, Plugin):
> continue
> except ValueError: # issubclass sucks
> continue # not a class
>
> thing() # probably needs error handling around this
>
> (I also moved the thing call out of the thing validation part, to allow
> you to distinguish between ValueErrors caused by issubclass and errors
> caused by things)
>
> (btw, another approach would be to use a metaclass to make Plugin
> classes register themselves on import)
>
> </F>
Thanks, that works much better. I've only been using python a couple of
months, so still getting to grips with how it treats classes/subclasses.
I'll have a look into metaclasses too, haven't stumbled upon those yet
at all.
Cheers,
Dave
- --
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
) <><
<>< (
Dave Challis )
____dsc at ecs.soton.ac.uk_______(___________________________________________
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFIibiHv26GZvAVVFERAgcmAJ4tmC7jp6TTb3Dx2Lw+rKSmJkcSLQCfYFsA
e0ZLOf8lQXhqHcz/Me8ok0E=
=Qcg7
-----END PGP SIGNATURE-----
More information about the Python-list
mailing list