Partial classes
Kay Schluehr
kay.schluehr at gmx.net
Wed Jul 19 07:46:10 EDT 2006
brianmce at gmail.com wrote:
> Sanjay wrote:
> > Hi All,
> >
> > Not being able to figure out how are partial classes coded in Python.
> >
> > Example: Suppose I have a code generator which generates part of a
> > business class, where as the custome part is to be written by me. In
> > ruby (or C#), I divide the code into two source files. Like this:
>
> I would do this by inheritance if really needed - what isn't working?
> That said, you can get this behaviour fairly easy with a metaclass:
>
> class PartialMeta(type):
> registry = {}
> def __new__(cls,name,bases,dct):
> if name in PartialMeta.registry:
> cls2=PartialMeta.registry[name]
> for k,v in dct.items():
> setattr(cls2, k, v)
> else:
> cls2 = type.__new__(cls,name,bases,dct)
> PartialMeta.registry[name] = cls2
> return cls2
>
> class PartialClass(object):
> __metaclass__=PartialMeta
This definition lacks a check for disjointness of the parts. No two
partial classes shall contain a method with the same name.
More information about the Python-list
mailing list