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 use: #generatedperson.py class Person(PartialClass): def foo(self): print "foo" #gandcraftedperson.py import generatedperson class Person(PartialClass): def bar(self): print "bar" and you should get similar behaviour. Caveats: I've used just the name to determine the class involved to be the same as your Ruby example. However, this means that any class in any namespace with the name Person and inheriting from PartialClass will be interpreted as the same - this might not be desirable if some other library code has a different Person object doing the same thing. Its easy to change to checking for some property instead - eg. have your handcrafted class have the line "__extends__=generatedperson.Person" , and check for it in the metaclass instead of looking in a name registry. Also, if two or more classes define the same name, the last one evaluated will overwrite the previous one. -- http://mail.python.org/mailman/listinfo/python-list