Partial 1.0 - Partial classes for Python

Thomas Heller theller at ctypes.org
Wed Feb 7 08:51:50 EST 2007


Martin v. Löwis schrieb:
> I'm happy to announce partial 1.0; a module to implement
> partial classes in Python. It is available from
> 
> http://cheeseshop.python.org/pypi/partial/1.0
> 
> A partial class is a fragment of a class definition;
> partial classes allow to spread the definition of
> a class over several modules. One location serves
> as the original definition of the class.
> 
> To extend a class original_module.FullClass with
> an additional function, one writes
> 
> from partial import *
> import original_module
> 
> class ExtendedClass(partial, original_module.FullClass):
>      def additional_method(self, args):
>          body
>      more_methods
> 
> This module is licensed under the Academic Free License v3.0.
> 
> Please send comments and feedback to martin at v.loewis.de

Nice idea.  I had to apply this change to make it work, though:

diff -u c:\python25\lib\site-packages\partial-1.0-py2.5.egg\partial.py.orig c:\python25\lib\site-packages\partial-1.0-py2.5.egg\partial.py
--- c:\python25\lib\site-packages\partial-1.0-py2.5.egg\partial.py.orig	Wed Feb 07 13:47:55 2007
+++ c:\python25\lib\site-packages\partial-1.0-py2.5.egg\partial.py	Wed Feb 07 13:47:55 2007
@@ -36,7 +36,7 @@
                 continue
             if k in base.__dict__ and not hasattr(v, '__replace'):
                 raise TypeError, "%s already has %s" % (repr(base), k)
-            base.__dict__[k] = v
+            setattr(base, k, v)
         # Return the original class
         return base

otherwise I get this:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\python25\lib\site-packages\partial-1.0-py2.5.egg\partial.py", line 39, in __new__
    base.__dict__[k] = v
TypeError: Error when calling the metaclass bases
    'dictproxy' object does not support item assignment


IIUC, wouldn't be 'partial.extend' or something like that be a better name
for the base class?

Thanks,
Thomas



More information about the Python-list mailing list