Partial 1.0 - Partial classes for Python

Thomas Heller theller at python.net
Thu Feb 8 14:15:47 EST 2007


Ziga Seilnacht schrieb:
> Thomas Heller wrote:
>>
>> Do you have a pointer to that post?
>>
> 
> I think that he was refering to this post:
> http://mail.python.org/pipermail/python-list/2006-December/416241.html
> 
> If you are interested in various implementations there is also this:
> http://mail.python.org/pipermail/python-list/2006-August/396835.html
> 
> and a module from PyPy:
> http://mail.python.org/pipermail/python-dev/2006-July/067501.html
> 
> which was moved to a new location:
> https://codespeak.net/viewvc/pypy/dist/pypy/tool/pairtype.py?view=markup
> 

Thanks for these links.  It seems they all (including Martin's partial) implementation
all use more or less the same trick (or hack ;-).

I agree with most of the posters is this thread that it is confusing to spread
the definition of a class over several places or files.

But, there are cases where the trick come in handy - when classes are created
not by class statements.

In ctypes, for example, a pointer type to a ctypes type is created by calling
the POINTER function which creates a new class.  When you have done this, the
usual way to add additional methods to the new class is by assigning them like this:

from ctypes import *

pointer_to_c_int = POINTER(c_int)

@classmethod
def from_param(cls, value):
    ... do something ...

pointer_to_c_int.from_param = from_param

IMO, using the tricky class in the recipes mentioned above, you can write instead:

class pointer_to_c_int(partial, POINTER(c_int)):
    @classmethod
    def from_param(cls, value):
        ... do something ...

Thomas



More information about the Python-list mailing list