Partial classes

Daniel Dittmar daniel.dittmar at sap.corp
Wed Jul 19 03:58:43 EDT 2006


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:
> 
> GeneratedPerson.rb
> Class Person
>   .
>   .
>   .
> 
> End Class
> 
> HandcraftedPerson.rb
> Class Person
>  .
>  .
>  .
> End Class
> 
> The intrepretor adds the code in both to compose the class Person.
> 
> What is the equivalent in Python? Inheriting is a way, but is not
> working in all scenerios.

# HandcraftedPerson.py
import GeneratedPerson

class Person:
     def somemethod (self):
         pass


GeneratedPerson.Person.somemethod = Person.somemethod

Using reflection to merge all methods of HandcraftedPerson.Person into 
GeneratedPerson.Person is left as an exercise.

Daniel



More information about the Python-list mailing list