Partial classes

Kay Schluehr kay.schluehr at gmx.net
Wed Jul 19 02:54:33 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.
>
> Thanks
> Sanjay

Python has no notion of a partial class because it is a pure compile
time construct. You might merge different class definitions by means of
a meta class that puts everything together but whether or not certain
methods in the merged class are available depends on which modules are
imported. This might give rise to a "virtual" or runtime module. It is
not a module that refers to a physical file on the disc but is a pure
runtime construct. When creating this module all physical modules that
define class fragments might be put together by means of the metaclass
mechanism.  I indeed used this construction to unify different access
points before I reimplemented it using partial classes in C# which are
very fine IMO.




More information about the Python-list mailing list